首页 > 代码库 > Log4Net详细配置

Log4Net详细配置

关于Log4Net配置主要分几步

第一步:下载log4net.dll(log4net官网:http://logging.apache.org/log4net/download_log4net.cgi)

第二步:在项目中添加对log4net.dll的引用

第三步:配置log4net

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<!--The config of System‘s default level - FATAL-->
<appender name="FATAL" type="log4net.Appender.RollingFileAppender">
<file value="http://www.mamicode.com/Log/fatal"/>
<appendToFile value="http://www.mamicode.com/true"/>
<rollingStyle value="http://www.mamicode.com/Composite" />
<maxSizeRollBackups value="http://www.mamicode.com/100" />
<maximumFileSize value="http://www.mamicode.com/102400KB" />
<staticLogFileName value="http://www.mamicode.com/false" />
<datePattern value="http://www.mamicode.com/_yyyyMMdd".log""/>
<param name="PrintFlag" value="http://www.mamicode.com/true" />
<ImmediateFlush value="http://www.mamicode.com/true" />
<encoding value="http://www.mamicode.com/utf-8"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="http://www.mamicode.com/[%p][%d{yyyy/MM/dd HH:mm:ss.fff}] [Thread=%thread] %class(%L) %M - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="http://www.mamicode.com/FATAL"/>
<param name="LevelMax" value="http://www.mamicode.com/FATAL"/>
</filter>
</appender>
<!--The config of System‘s default level - ERROR-->
<appender name="ERROR" type="log4net.Appender.RollingFileAppender">
<file value="http://www.mamicode.com/log/error"/>
<appendToFile value="http://www.mamicode.com/true"/>
<rollingStyle value="http://www.mamicode.com/Composite" />
<maxSizeRollBackups value="http://www.mamicode.com/100" />
<maximumFileSize value="http://www.mamicode.com/102400KB" />
<staticLogFileName value="http://www.mamicode.com/false" />
<datePattern value="http://www.mamicode.com/_yyyyMMdd".log""/>
<param name="PrintFlag" value="http://www.mamicode.com/true" />
<ImmediateFlush value="http://www.mamicode.com/true" />
<encoding value="http://www.mamicode.com/utf-8"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="http://www.mamicode.com/[%p][%d{yyyy/MM/dd HH:mm:ss.fff}] [Thread=%thread] %class(%L) %M - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="http://www.mamicode.com/ERROR"/>
<param name="LevelMax" value="http://www.mamicode.com/ERROR"/>
</filter>
</appender>
<!--The config of System‘s default level - WARN-->
<appender name="WARN" type="log4net.Appender.RollingFileAppender">
<file value="http://www.mamicode.com/log/warn"/>
<appendToFile value="http://www.mamicode.com/true"/>
<rollingStyle value="http://www.mamicode.com/Composite" />
<maxSizeRollBackups value="http://www.mamicode.com/100" />
<maximumFileSize value="http://www.mamicode.com/102400KB" />
<staticLogFileName value="http://www.mamicode.com/false" />
<datePattern value="http://www.mamicode.com/_yyyyMMdd".log""/>
<param name="PrintFlag" value="http://www.mamicode.com/true" />
<ImmediateFlush value="http://www.mamicode.com/true" />
<encoding value="http://www.mamicode.com/utf-8"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="http://www.mamicode.com/[%p][%d{yyyy/MM/dd HH:mm:ss.fff}] [Thread=%thread] %class(%L) %M - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="http://www.mamicode.com/WARN"/>
<param name="LevelMax" value="http://www.mamicode.com/WARN"/>
</filter>
</appender>
<!--The config of System‘s default level - INFO-->
<appender name="INFO" type="log4net.Appender.RollingFileAppender">
<file value="http://www.mamicode.com/log/info"/>
<appendToFile value="http://www.mamicode.com/true"/>
<rollingStyle value="http://www.mamicode.com/Composite" />
<maxSizeRollBackups value="http://www.mamicode.com/100" />
<maximumFileSize value="http://www.mamicode.com/102400KB" />
<staticLogFileName value="http://www.mamicode.com/false" />
<datePattern value="http://www.mamicode.com/_yyyyMMdd".log""/>
<param name="PrintFlag" value="http://www.mamicode.com/true" />
<ImmediateFlush value="http://www.mamicode.com/true" />
<encoding value="http://www.mamicode.com/utf-8"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="http://www.mamicode.com/[%p][%d{yyyy/MM/dd HH:mm:ss.fff}] [Thread=%thread] %class(%L) %M - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="http://www.mamicode.com/INFO"/>
<param name="LevelMax" value="http://www.mamicode.com/INFO"/>
</filter>
</appender>
<!--The config of System‘s default level - DEBUG-->
<appender name="DEBUG" type="log4net.Appender.RollingFileAppender">
<file value="http://www.mamicode.com/log/debug"/>
<appendToFile value="http://www.mamicode.com/true"/>
<rollingStyle value="http://www.mamicode.com/Composite" />
<maxSizeRollBackups value="http://www.mamicode.com/100" />
<maximumFileSize value="http://www.mamicode.com/102400KB" />
<staticLogFileName value="http://www.mamicode.com/false" />
<datePattern value="http://www.mamicode.com/_yyyyMMdd".log""/>
<param name="PrintFlag" value="http://www.mamicode.com/false" />
<ImmediateFlush value="http://www.mamicode.com/true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="http://www.mamicode.com/[%p][%d{yyyy/MM/dd HH:mm:ss.fff}] [Thread=%thread] %class(%L) %M - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="http://www.mamicode.com/DEBUG"/>
<param name="LevelMax" value="http://www.mamicode.com/DEBUG"/>
</filter>
</appender>


<!--The basic config-->
<root name="SystemLogger">
<!--The whole switch.(Whose the level is greater than or equals to this will be display)-->
<level value="http://www.mamicode.com/ALL" />
<appender-ref ref="DEBUG" />
<appender-ref ref="INFO" />
<appender-ref ref="WARN" />
<appender-ref ref="ERROR" />
<appender-ref ref="FATAL" />

</root>
</log4net>

第四步:在程序运行之前,加载log4net的配置

log4net.Config.XmlConfigurator.Configure("log4net配置文件的路径");

第五步:使用log4net记录日志

ILog logger = log4net.LogManager.GetLogger("SystemLogger");

log4net.Info("info");

log4net.Error("error");

.....

Log4Net详细配置