ASP.NET MVC使用Log4Net記錄異常日志并跳轉(zhuǎn)到靜態(tài)頁
本篇體驗(yàn)在ASP.NET MVC 4中使用Log4Net記錄日志。
通過NuGet安裝Log4Net。
需求是:當(dāng)出錯時導(dǎo)向到Error.html靜態(tài)頁面,Log4Net記錄錯誤信息。
大致的思路是:
1、寫一個記錄日志的接口
2、實(shí)現(xiàn)記錄日志接口的類,用Log4Net的API實(shí)現(xiàn)
3、在Web.config中配置Log4Net
4、在Global.asax中注冊Log4Net
5、自定義一個出錯頁,以便在出錯時導(dǎo)向到該靜態(tài)頁面
6、ASP.NET MVC默認(rèn)的異常過濾器是HandleErrorAttribute,我們需要自定義一個繼承HandleErrorAttribute的過濾器,并把自定義的過濾器注冊到全局過濾器中去
首先定義一個記錄日志的接口。
public interface ILoggerService {void Info(string message);void Warn(string message);void Debug(string message);void Error(string message);void Error(Exception ex);void Fatal(string message);void Fatal(Exception ex); }
實(shí)現(xiàn)ILoggerService,使用用Log4Net的API實(shí)現(xiàn)。
public class LogHelper : ILoggerService { private ILog _logger; public LogHelper() { _logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);}public void Info(string message) { _logger.Info(message);}public void Warn(string message) { _logger.Warn(message);}public void Debug(string message) { _logger.Debug(message);}public void Error(string message) { _logger.Error(message);}public void Error(Exception ex) { _logger.Error(ex.Message, ex);}public void Fatal(string message) { _logger.Fatal(message);}public void Fatal(Exception ex) { _logger.Fatal(ex.Message, ex);} }
在Web.config中配置Log4Net。
<configuration> <configSections> ...... <!--日志的配置--> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4Net" /> </configSections> ...... <!--日志的配置開始--> <log4net> <root> <level value="ALL" /> <appender-ref ref="SysAppender" /> </root> <logger name="WebLogger"> <!--配置日志的級別,低于此級別的就不寫到日志里面去--> <level value="DEBUG" /> </logger> <!--系統(tǒng)日志的格式--> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net"> <param name="File" value="App_Data/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"><!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />--> <conversionPattern value="{%level}%date{MM/dd HH:mm:ss} - %message%newline%newline"/> </layout> </appender> <!--控制臺日志的格式--> <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net"> <layout type="log4net.Layout.PatternLayout,log4net"><param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> </log4net> <!--日志的配置結(jié)束--></configuration>
在全局文件Global.asax中注冊Log4Net。
public class MvcApplication : System.Web.HttpApplication {protected void Application_Start(){ AreaRegistration.RegisterAllAreas(); ...... //讀取日志 如果使用log4net,應(yīng)用程序一開始的時候,都要進(jìn)行初始化配置 log4net.Config.XmlConfigurator.Configure();} }
ASP.NET MVC默認(rèn)的異常過濾器是HandleErrorAttribute,我們需要自定義,繼承該類。
public class MyHandleExceptionAttribute : HandleErrorAttribute {public override void OnException(ExceptionContext filterContext){ base.OnException(filterContext); var log = new LogHelper(); log.Error("被系統(tǒng)過濾捕獲的異常" + filterContext.Exception); filterContext.HttpContext.Response.Redirect("/Error.html");} }
自定義的異常過濾器當(dāng)讓要注冊到全局過濾器中去。打開App_Start文件夾中的FilterConfig類,修改如下:
public class FilterConfig {public static void RegisterGlobalFilters(GlobalFilterCollection filters){ //filters.Add(new HandleErrorAttribute()); filters.Add(new MyHandleExceptionAttribute());} }
在HomeController中故意留一個錯誤。
public class HomeController : Controller {public ActionResult Index(){ int a = 10; int b = 0; var result = a/b; return View();} }
在項(xiàng)目根文件夾下頂一個Error.html靜態(tài)文件,用來呈現(xiàn)錯誤提示信息。
當(dāng)瀏覽器請求Home/Index視圖,導(dǎo)向到Error.html出錯頁,Log4Net自動為我們在App_Data目錄下記錄了異常信息。
到此這篇關(guān)于ASP.NET MVC使用Log4Net記錄異常日志并跳轉(zhuǎn)到靜態(tài)頁的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
相關(guān)文章:
1. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容2. ASP.NET MVC使用Session會話保持表單狀態(tài)3. ASP.NET MVC把數(shù)據(jù)庫中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字4. ASP.NET MVC實(shí)現(xiàn)本地化和全球化5. ASP.NET MVC遍歷驗(yàn)證ModelState的錯誤信息6. ASP.NET MVC使用異步Action的方法7. ASP.NET MVC解決上傳圖片臟數(shù)據(jù)的方法8. log4net在Asp.net MVC4中的使用過程9. ASP.NET MVC實(shí)現(xiàn)登錄后跳轉(zhuǎn)到原界面10. ASP.NET MVC使用正則表達(dá)式驗(yàn)證手機(jī)號碼
