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.配置文件
注意加粗部分
- <?xml version="1.0"?>
- <configuration>
- <configSections>
- <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
- <sectionGroup name="common">
- <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
- </sectionGroup>
- </configSections>
- <common>
- <logging>
- <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211">
- <arg key="configType" value="INLINE"/>
- </factoryAdapter>
- </logging>
- </common>
- <log4net>
- <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
- <layout type="log4net.Layout.PatternLayout">
- <conversionPattern value="%d [%t] %-5p %l - %m%n"/>
- </layout>
- </appender>
- <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
- <layout type="log4net.Layout.PatternLayout">
- <conversionPattern value="%d [%t] %-5p %l - %m%n"/>
- </layout>
- </appender>
- <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
- <param name="File" value="Log/" />
- <!--<datePattern value="yyyyMMdd".txt"" />-->
- <appendToFile value="true"/>
- <!--Make the rolling file name with the date and size-->
- <rollingStyle value="Composite"/>
- <datePattern value="yyyy-MM-dd".txt""/>
- <maxSizeRollBackups value="100"/>
- <maximumFileSize value="2MB"/>
- <PreserveLogFileNameExtension value="true"/>
- <staticLogFileName value="false"/>
- <layout type="log4net.Layout.PatternLayout">
- <param name="ConversionPattern" value="%d %-5p %m%n"/>
- </layout>
- </appender>
- <root>
- <level value="DEBUG"/>
- <appender-ref ref="ConsoleAppender"/>
- <appender-ref ref="RollingFileAppender"/>
- </root>
- </log4net>
- <runtime>
- <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
- <dependentAssembly>
- <assemblyIdentity name="Common.Logging.Core" publicKeyToken="af08829b84f0328e" culture="neutral"/>
- <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
- </dependentAssembly>
- <dependentAssembly>
- <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral"/>
- <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
- </dependentAssembly>
- </assemblyBinding>
- </runtime>
- </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