Log4net 配置文件
需要先引用如下dll 文件:
log4net.dll
Common.Logging.Log4Net.dll
Common.Logging.dll
PS:若编译后需将dll文件拷贝到生成目录下(默认\bin\Debug)
<?xml version="1.0"?> <configuration> <configSections> <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <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.Log4net"> <arg key="configType" value="INLINE"/> </factoryAdapter> </logging> </common> <log4net> <appender name="InfoFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log/"/> <appendToFile value="true"/> <param name="DatePattern" value="yyyyMMdd".txt""/> <rollingStyle value="Date"/> <maxSizeRollBackups value="100"/> <maximumFileSize value="1024KB"/> <staticLogFileName value="false"/> <Encoding value="UTF-8"/> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="INFO"/> <param name="LevelMax" value="INFO"/> </filter> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %-5level %logger - %message%newline"/> </layout> </appender> <appender name="ErrorFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log/error.txt"/> <appendToFile value="true"/> <rollingStyle value="Size"/> <maxSizeRollBackups value="100"/> <maximumFileSize value="10240KB"/> <staticLogFileName value="true"/> <Encoding value="UTF-8"/> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="WARN"/> <param name="LevelMax" value="FATAL"/> </filter> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %-5level %logger - %message%newline"/> </layout> </appender> <root> <level value="INFO"/> <appender-ref ref="InfoFileAppender"/> <appender-ref ref="ErrorFileAppender"/> </root> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
调用代码:
public class MyScheduler { private ILog log; public MyScheduler() { log = LogManager.GetLogger(GetType()); } public void TestWriteLog() { log.Info("TestWriteLog"); log.Error("错误信息"); } }