Comon.Logging与Log4net联合使用

1.摘要

Common.Logging定义了一种接口的公共接口,尤其在Quartz.net中作为接口型的组件,而具体则使用log4net,nlog等组件。

2.使用步骤

Install-Package Common.Logging

安装公共日志组件

Install-Package log4net

安装log4net

Install-Package Common.Logging.Log4Net1211

安log4net对Common.Logging的适配库

3.配置文件

注意加粗部分

  1. <?xml version="1.0"?>
  2. <configuration>
  3.   <configSections>
  4.     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  5.     <sectionGroup name="common">
  6.       <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
  7.     </sectionGroup>
  8.   </configSections>
  9.   <common>
  10.     <logging>
  11.       <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211">
  12.         <arg key="configType" value="INLINE"/>
  13.       </factoryAdapter>
  14.     </logging>
  15.   </common>
  16.   <log4net>
  17.     <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  18.       <layout type="log4net.Layout.PatternLayout">
  19.         <conversionPattern value="%d [%t] %-5p %l - %m%n"/>
  20.       </layout>
  21.     </appender>
  22.     <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
  23.       <layout type="log4net.Layout.PatternLayout">
  24.         <conversionPattern value="%d [%t] %-5p %l - %m%n"/>
  25.       </layout>
  26.     </appender>
  27.     <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  28.       <param name="File" value="Log/" />
  29.       <!--<datePattern value="yyyyMMdd&quot;.txt&quot;" />-->
  30.       <appendToFile value="true"/>
  31.  
  32.       <!--Make the rolling file name with the date and size-->
  33.       <rollingStyle value="Composite"/>
  34.       <datePattern value="yyyy-MM-dd&quot;.txt&quot;"/>
  35.       <maxSizeRollBackups value="100"/>
  36.       <maximumFileSize value="2MB"/>
  37.  
  38.       <PreserveLogFileNameExtension value="true"/>
  39.       <staticLogFileName value="false"/>
  40.       <layout type="log4net.Layout.PatternLayout">
  41.         <param name="ConversionPattern" value="%d %-5p %m%n"/>
  42.       </layout>
  43.     </appender>
  44.     <root>
  45.       <level value="DEBUG"/>
  46.       <appender-ref ref="ConsoleAppender"/>
  47.       <appender-ref ref="RollingFileAppender"/>
  48.     </root>
  49.   </log4net>
  50.   <runtime>
  51.     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  52.       <dependentAssembly>
  53.         <assemblyIdentity name="Common.Logging.Core" publicKeyToken="af08829b84f0328e" culture="neutral"/>
  54.         <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
  55.       </dependentAssembly>
  56.       <dependentAssembly>
  57.         <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral"/>
  58.         <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
  59.       </dependentAssembly>
  60.     </assemblyBinding>
  61.   </runtime>
  62. </configuration>

4.样例代码

using Common.Logging;

。。。。。。

ILog log = LogManager.GetLogger(type);

if (log.IsDebugEnabled)

{

string strText = GetMessage("DEBUG", className, methodName, message);

log.Debug(strText);

}

。。。。。。

参考:http://www.tuicool.com/articles/JvIbM3

上一篇:MongoDB--关于数据库及选择MongoDB的原因


下一篇:Java课程设计报告——学生成绩管理系统