开源日志记录组件Log4Net的使用
1、在项目中引入Log4Net程序集
2、在项目配置文件中添加Log4Net配置节
Log4Net的基本配置
1 <configuration> 2 <configSections> 3 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 4 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 5 <!--Log4Net配置--> 6 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/><!--Log4Net配置由Log4NetConfigurationSectionHandler节处理--> 7 </configSections> 8 <log4net> 9 <!-- Define some output appenders --> 10 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"><!--设置以滚动文件形式记录日志--> 11 <file value="test.txt"/><!--日志形成的文本名称--> 12 <appendToFile value="true"/><!--文件追加--> 13 <maxSizeRollBackups value="10"/><!--最多形成10个日志文件--> 14 <maximumFileSize value="1024KB"/><!--每个日志文件大小为1024KB--> 15 <rollingStyle value="Size"/><!--日志文件以文件大小生成,一个日志文件到1024KB之后形成下一个--> 16 <staticLogFileName value="true"/><!--日志文件名固定死的--> 17 <layout type="log4net.Layout.PatternLayout"><!--日志文本显示格式--> 18 <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/> 19 </layout> 20 </appender> 21 <root> 22 <level value="DEBUG"/><!--等级--> 23 <appender-ref ref="RollingLogFileAppender"/> 24 </root> 25 </log4net> 26 <!--Log4Net配置结束--> 27 </configuration>
Log4Net专业配置(当前项目.Net MVC)
1 <configuration> 2 <configSections> 3 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 4 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 5 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 6 <!--Log4Net配置--> 7 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 8 </configSections> 9 10 <log4net> 11 <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> 12 <!-- Set root logger level to ERROR and its appenders --> 13 <root> 14 <level value="ALL"/> 15 <appender-ref ref="SysAppender"/> 16 </root> 17 18 <!-- Print only messages of level DEBUG or above in the packages --> 19 <logger name="WebLogger"> 20 <level value="ERROR"/> 21 </logger> 22 23 <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" > 24 <param name="File" value="App_Data/" /><!--将日志文件放在App_Data下,客户端通过浏览器地址不能直接打开,增加了安全性--> 25 <param name="AppendToFile" value="true" /> 26 <param name="RollingStyle" value="Date" /> 27 <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /><!--日志文件名以日期天数为名称--> 28 <param name="StaticLogFileName" value="false" /><!--不起用静态命名方式--> 29 <layout type="log4net.Layout.PatternLayout,log4net"><!--格式--> 30 <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> 31 <param name="Header" value=" ----------------------header-------------------------- " /> 32 <param name="Footer" value=" ----------------------footer-------------------------- " /> 33 </layout> 34 </appender> 35 <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net"><!--异常信息--> 36 <layout type="log4net.Layout.PatternLayout,log4net"> 37 <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> 38 </layout> 39 </appender> 40 </log4net> 41 <!--Log4Net配置结束--> 42 </configuration>
3、在应用程序开始处Application_Start()方法中写 log4net.Config.XmlConfigurator.Configure();读取Log4Net配置信息
通过Log4Net将异常信息写到磁盘上记录日志
1 protected void Application_Start() 2 { 3 log4net.Config.XmlConfigurator.Configure();//读取Log4Net配置信息 4 AreaRegistration.RegisterAllAreas(); 5 6 WebApiConfig.Register(GlobalConfiguration.Configuration); 7 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 8 RouteConfig.RegisterRoutes(RouteTable.Routes); 9 BundleConfig.RegisterBundles(BundleTable.Bundles); 10 11 string fileLogPath = Server.MapPath("/Log/"); 12 13 ThreadPool.QueueUserWorkItem((a) => 14 { 15 while (true) 16 { 17 if (MyExceptionAttribute.ExceptionQueue.Count > 0) 18 { 19 Exception ex= MyExceptionAttribute.ExceptionQueue.Dequeue(); 20 ILog logger = LogManager.GetLogger("errorMsg");//Log4Net创建异常信息 21 logger.Error(ex.ToString());//将异常信息写到磁盘上 22 } 23 else 24 { 25 Thread.Sleep(3000); 26 } 27 } 28 29 30 },fileLogPath); 31 }
4、日志文件