Common.Logging+(log4net/NLog/)
common logging是一个通用日志接口,log4net是一个强大的具体实现,也可以用其它不同的实现,如EntLib的日志、NLog等。
Common.Logging可以通过修改配置,更换不同的日志实现类,而不需要修改任何代码。
.net 项目使用CommonLogging:
1、添加必要的程序集:Common.Logging.dll、Common.Logging.Core.dll、Common.Logging.log4net1215.dll、log4net.dll
其中需要特别注意Common.Logging.log4net1215.dll、log4net.dll的版本必须匹配,建议使用VS的NuGet添加引用,添加时有详细的说明。
2、写配置文件
<?xml version="1.0" encoding="utf-8" ?> <factoryAdapter> <!-- choices are INLINE, FILE, FILE-WATCH, EXTERNAL--> <!-- otherwise BasicConfigurer.Configure is used --> <!-- log4net configuration file is specified with key configFile--> <arg key="configType" value="FILE-WATCH" /> <arg key="configFile" value="~/Config/Log4Net.xml" /> <!--与主程序共用日志文件,则传true,日志初始化时不创建目录结构,只构造几个Logger实例--> <arg key="shareLogFile" value="true" /> <arg key="regenerateConfigFile" value="false" /> <!-- 0 to 6 (1 Debug 4 Error)--> <arg key="Level" value="4" /> </factoryAdapter> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /> </sectionGroup> <common> <logging> <!--<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1213">--> <factoryAdapter type="M2SH.Common.Utilities.Logs.EHLog4NetLoggerFactoryAdapter, M2SH.Common"> <!-- choices are INLINE, FILE, FILE-WATCH, EXTERNAL--> <!-- otherwise BasicConfigurer.Configure is used --> <!-- log4net configuration file is specified with key configFile--> <arg key="configType" value="FILE-WATCH" /> <arg key="configFile" value="~/Config/Log4Net.xml" /> <arg key="regenerateConfigFile" value="true" /> <!-- 0 to 6 (1 Debug 4 Error)--> <arg key="Level" value="4" /> </factoryAdapter> </logging> </common>
<arg key="configType" value="FILE-WATCH" />
<arg key="configFile" value="~/log4net.config" />
以上配置表示将log4net的配置放到单独的文件中
<!--<arg key="configType" value="INLINE" />-->
这个表示log4net的配置直接写在配置文件里
log4net.config文件的内容如下:
<?xml version="1.0" encoding="utf-8" ?> <log4net debug="false"> <appender name="AspNetTraceAppender" type="log4net.Appender.AspNetTraceAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%thread] %-5level - %message" /> </layout> </appender> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file>Logs/{0}/Sys/sys</file> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyy-MM-dd.'log'" /> <maxSizeRollBackups value="2" /> <maximumFileSize value="5M" /> <!--最小锁定模型以允许多个进程可以写入同一个文件--> <!--<lockingModel value="log4net.Appender.FileAppender.MinimalLock"/>--> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date |[%thread] |%-5level |%logger |%message%newline" /> </layout> </appender> <appender name="RollingFileAppenderBiz" type="log4net.Appender.RollingFileAppender"> <file>Logs/{0}/Biz/biz</file> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyy-MM-dd.'log'" /> <maxSizeRollBackups value="2" /> <maximumFileSize value="5M" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="!!! %date |[%thread] |%-5level |%message%newline" /> </layout> </appender> <appender name="RollingFileAppenderNetReq" type="log4net.Appender.RollingFileAppender"> <file>Logs/{0}/NetReq/netreq</file> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyy-MM-dd.'log'" /> <maxSizeRollBackups value="2" /> <maximumFileSize value="5M" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="!!! %date |[%thread] |%-5level |%message%newline" /> </layout> </appender> <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%-5level %M - %message%newline" /> </layout> </appender> <appender name="MemoryAppender" type="log4net.Appender.MemoryAppender"> <!-- simple appender to get results to UI --> </appender> <!-- Set default logging level to DEBUG --> <root> <level value="DEBUG" /> <appender-ref ref="TraceAppender" /> <appender-ref ref="RollingFileAppender" /> </root> <!--业务日志--> <logger name="BizLogger" additivity="false"> <level value="Info" /> <appender-ref ref="RollingFileAppenderBiz" /> </logger> <!--网络请求日志--> <logger name="NetReqLogger" additivity="false"> <level value="DEBUG" /> <appender-ref ref="RollingFileAppenderNetReq" /> </logger> </log4net>
配置文件中定义了 AspNetTraceAppender,RollingFileAppender,RollingFileAppenderBiz,RollingFileAppenderNetReq,TraceAppender,MemoryAppender六个日志记录器。
一个root日志记录器,log4net只允许一个root日志记录器,用于做全局日志输出
<root> <level value="DEBUG" /> <appender-ref ref="TraceAppender" /> <appender-ref ref="RollingFileAppender" /> </root>
两个logger(注:只有配置了 appender 的logger 才会输出日志内容)
<!--业务日志--> <logger name="BizLogger" additivity="false"> <level value="Info" /> <appender-ref ref="RollingFileAppenderBiz" /> </logger> <!--网络请求日志--> <logger name="NetReqLogger" additivity="false"> <level value="DEBUG" /> <appender-ref ref="RollingFileAppenderNetReq" /> </logger>
附加器的配置要点:
1、name="DefaultRollingLogFileAppender"
定义附加器的名称,方便日志记录器调用
2、type="log4net.Appender.RollingFileAppender, log4net"
附加器的类型,附加器的具体实现类,log4net提供多种日志存储(附加)方式,在这里就是不同的附加器实现类。
详细的附加器类型可以查看官方文档中的appender章节:
http://logging.apache.org/log4net/release/manual/introduction.html