<configSections> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /> </sectionGroup> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <common> <!--[DEBUG < INFO < WARN < ERROR < FATAL]--> <logging> <!--<factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging"> <arg key="level" value="WARN" /> <arg key="showLogName" value="true" /> <arg key="showDataTime" value="true" /> <arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" /> </factoryAdapter>--> <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1215"> <!--FILE FILE-WATCH INLINE EXTERNAL--> <!--<arg key="configType" value="INLINE" />--> <arg key="configType" value="FILE-WATCH" /> <arg key="configFile" value="~/log4net.config" /> <!--log4net使用独立的配置文件--> </factoryAdapter> </logging> </common>
<arg key="configType" value="FILE-WATCH" />
<?xml version="1.0" encoding="utf-8" ?> <log4net> <!--<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" /> </layout> </appender>--> <appender name="InfoRollingLogFileAppender" type="log4net.Appender.RollingFileAppender, log4net"> <param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value="logs/" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="'Info-'yyyy-MM-dd.'log'" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="10" /> <param name="MaximumFileSize" value="5MB" /> <param name="RollingStyle" value="Date" /> <!--Size ==- Date--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="DEBUG"/> <levelMax value="WARN"/> </filter> </appender> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender, log4net"> <param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value="logs/" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="'Error-'yyyy-MM-dd.'log'" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="10" /> <param name="MaximumFileSize" value="5MB" /> <param name="RollingStyle" value="Date" /> <!--Size ==- Date--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="ERROR"/> <levelMax value="FATAL"/> </filter> </appender> <appender name="DefaultRollingLogFileAppender" type="log4net.Appender.RollingFileAppender, log4net"> <param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value="logs/" /><!--文件保存的路径--> <param name="StaticLogFileName" value="false" /><!--文件名动态生成--> <param name="DatePattern" value="'Default-'yyyy-MM-dd.'log'" /><!--文件名命名规则--> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="10" /> <param name="MaximumFileSize" value="5MB" /><!--文件超过5M将创建新文件--> <param name="RollingStyle" value="Date" /> <!--Size ==- Date--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /><!--日志内容格式--> </layout> <filter type="log4net.Filter.LevelRangeFilter"><!--日志级别过滤器:此附加器只附加指定级别的日志--> <levelMin value="DEBUG"/> <levelMax value="FATAL"/> </filter> </appender> <root> <level value="ALL" /> <appender-ref ref="RollingLogFileAppender" /> <appender-ref ref="InfoRollingLogFileAppender" /> </root> <logger name="MyApp.DataAccessLayer"> <level value="DEBUG" /> </logger> <!-- ALL DEBUG INFO WARN ERROR FATAL OFF [DEBUG < INFO < WARN < ERROR < FATAL] 大范围 到 小范围--> <logger name="LogDemoWeb._Default" debug="false"> <level value="ALL" /> <appender-ref ref="DefaultRollingLogFileAppender" /> </logger> </log4net>
这个文件建议从下往上读,上文红色部分是日志记录器一共三个:
一个root日志记录器,log4net只允许一个root日志记录器,用于做全局日志输出
<root> <level value="ALL" /> <appender-ref ref="RollingLogFileAppender" /> <appender-ref ref="InfoRollingLogFileAppender" /> </root>
其中: <level value="ALL" />表示记录器会将所有级别的日志提交给这里配置的附加器
这里配置了两个附加器:RollingLogFileAppender和InfoRollingLogFileAppender(配置文件中的appender节点)
两个logger:
第一个:
<logger name="MyApp.DataAccessLayer"> <level value="DEBUG" /> </logger>
没有配置appender(附加器)将不会输出日志
第二个:
<logger name="LogDemoWeb._Default" debug="false"> <level value="ALL" /> <appender-ref ref="DefaultRollingLogFileAppender" /> </logger>
配置了DefaultRollingLogFileAppender附加器,将按上面对应的附加器输出日志。
附加器(appender)可以定义多个,它只是一个声明,是否使用还是要看日志记录器是否引用了。
这个配置文件中DefaultRollingLogFileAppender 附加器(appender)的配置示例如下:
<appender name="DefaultRollingLogFileAppender" type="log4net.Appender.RollingFileAppender, log4net"> <param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value="logs/" /><!--文件保存的路径--> <param name="StaticLogFileName" value="false" /><!--文件名动态生成--> <param name="DatePattern" value="'Default-'yyyy-MM-dd.'log'" /><!--文件名命名规则--> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="10" /> <param name="MaximumFileSize" value="5MB" /><!--文件超过5M将创建新文件--> <param name="RollingStyle" value="Date" /> <!--Size ==- Date--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /><!--日志内容格式--> </layout> <filter type="log4net.Filter.LevelRangeFilter"><!--日志级别过滤器:此附加器只附加指定级别的日志--> <levelMin value="DEBUG"/> <levelMax value="FATAL"/> </filter> </appender>
附加器的配置要点:
1、name="DefaultRollingLogFileAppender"
定义附加器的名称,方便日志记录器调用
2、type="log4net.Appender.RollingFileAppender, log4net"
附加器的类型,附加器的具体实现类,log4net提供多种日志存储(附加)方式,在这里就是不同的附加器实现类。
详细的附加器类型可以查看官方文档中的appender章节:
http://logging.apache.org/log4net/release/manual/introduction.html
做好以上配置后代码就相当简单了
一般我是这样写的:
/// <summary> /// Common.Logging日志示例 /// </summary> public class LogDemoBLL1 { //获取ILog实例 public static ILog logObj = Common.Logging.LogManager.GetLogger<LogDemoBLL1>(); public LogDemoBLL1() { } public void Method1() { logObj.Trace("This is a Trace Log!"); logObj.Debug("This is a Debug Log!"); logObj.Info("This is a Info Log!"); logObj.Warn("This is a Warn Log!"); //将在Info-yyyy-MM-dd.log输出以下内容(根据配置输出有可能不一样): //2016-08-18 21:28:44,095 [10] DEBUG LogDemo.BLL.LogDemoBLL1 - This is a Debug Log! //2016-08-18 21:28:44,101 [10] INFO LogDemo.BLL.LogDemoBLL1 - This is a Info Log! //2016-08-18 21:28:44,101 [10] WARN LogDemo.BLL.LogDemoBLL1 - This is a Warn Log! logObj.Error("This is a Error Log!"); logObj.Fatal("This is a Fatal Log!"); //将在Error-yyyy-MM-dd.log输出以下内容(根据配置输出有可能不一样): //2016-08-18 21:28:44,101 [10] ERROR LogDemo.BLL.LogDemoBLL1 - This is a Error Log! //2016-08-18 21:28:44,102 [10] FATAL LogDemo.BLL.LogDemoBLL1 - This is a Fatal Log! try { throw new Exception("Throw an Exception For Test"); } catch (Exception ex) { //异常日志 logObj.Error(ex); //将在Error-yyyy-MM-dd.log输出以下内容(根据配置输出有可能不一样): //2016-08-18 21:28:44,106 [10] ERROR LogDemo.BLL.LogDemoBLL1 - System.Exception: Throw an Exception For Test // 在 LogDemo.BLL.LogDemoBLL1.Method1() 位置 E:\MyWork\CSharpProject\CSharp\LogDemo\LogDemo\LogDemo\BLL\LogDemoBLL1.cs:行号 34 } } }
如果有需要log4net还可以通过log4net.Appender.MemoryAppender类将日志显示在程序界面上。具体用法就不详细介绍了。
关于log4net的详细介绍可以看下面的文章:
http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html
Common.Logging官方网站:
http://commons.apache.org/proper/commons-logging/
Common.Logging中文介绍:
http://www.cnblogs.com/chen-whutgis/p/4129271.html
log4net官方网站
http://logging.apache.org/log4net/release/manual/introduction.html
Log4net的配置学习与总结
http://blog.csdn.net/wziyx513225244/article/details/6628627
另外安利一些高级打开方式:
Log4net 写文件日志与数据库日志
http://blog.csdn.net/manbufenglin/article/details/7168472
程序的记事本--log4net:这里介绍了常有的日志管理框架,值得一看。
http://blog.csdn.net/zhang_xinxiu/article/details/40649791