首页 > 代码库 > log4net
log4net
程序启动:
#region Log4net注册 private void RegisterLog4net() { try { string configfile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config"); if (!System.IO.File.Exists(configfile)) { return; } log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configfile)); log = LogManager.GetLogger(this.GetType()); } catch { } } #endregion
配置文件:log4net.config
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="http://www.mamicode.com/log/Debug.log"/>
<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/1MB"/>
<staticLogFileName value="http://www.mamicode.com/false"/>
<param name="DatePattern" value="http://www.mamicode.com/yyyyMM//yyyyMMdd//"Service.log""/>
<layout type="log4net.Layout.PatternLayout">
<header value="http://www.mamicode.com/************ System Start **********
"/>
<footer value="http://www.mamicode.com/************ System End ************
"/>
<conversionPattern value="http://www.mamicode.com/%d %-5p [%c] (line:%L) %n%m%n%n"/>
</layout>
</appender>
<appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="http://www.mamicode.com/log/Info.log"/>
<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/1MB"/>
<staticLogFileName value="http://www.mamicode.com/false"/>
<param name="DatePattern" value="http://www.mamicode.com/yyyyMM//yyyyMMdd//"Service.Info.log""/>
<layout type="log4net.Layout.PatternLayout">
<header value="http://www.mamicode.com/************ System Start **********
"/>
<footer value="http://www.mamicode.com/************ System End ************
"/>
<conversionPattern value="http://www.mamicode.com/#%d %-5p [%c] (line:%L) %n%m%n%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>
<appender name="WarnRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="http://www.mamicode.com/log/Warn.log"/>
<appendToFile value="http://www.mamicode.com/true"/>
<rollingStyle value="http://www.mamicode.com/Composite"/>
<param name="DatePattern" value="http://www.mamicode.com/yyyyMM//yyyyMMdd//"Service.Warn.log""/>
<maxSizeRollBackups value="http://www.mamicode.com/100"/>
<maximumFileSize value="http://www.mamicode.com/1MB"/>
<staticLogFileName value="http://www.mamicode.com/false"/>
<layout type="log4net.Layout.PatternLayout">
<header value="http://www.mamicode.com/************ System Start **********
"/>
<footer value="http://www.mamicode.com/************ System End ************
"/>
<conversionPattern value="http://www.mamicode.com/!%d %-5p [%c] (line:%L) %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>
<appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="http://www.mamicode.com/log/Error.log"/>
<appendToFile value="http://www.mamicode.com/true"/>
<rollingStyle value="http://www.mamicode.com/Composite"/>
<param name="DatePattern" value="http://www.mamicode.com/yyyyMM//yyyyMMdd//"Service.Error.log""/>
<maxSizeRollBackups value="http://www.mamicode.com/100"/>
<maximumFileSize value="http://www.mamicode.com/1MB"/>
<staticLogFileName value="http://www.mamicode.com/false"/>
<layout type="log4net.Layout.PatternLayout">
<header value="http://www.mamicode.com/************ System Start **********
"/>
<footer value="http://www.mamicode.com/************ System End ************
"/>
<conversionPattern value="http://www.mamicode.com/x!!%d %-5p [%c] (line:%L) %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/FATAL"/>
</filter>
</appender>
<root>
<level value="http://www.mamicode.com/ALL"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="InfoRollingFileAppender"/>
<appender-ref ref="WarnRollingFileAppender"/>
<appender-ref ref="ErrorRollingFileAppender"/>
</root>
</log4net>
</configuration>