程序启动:
#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="./log/Debug.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<maxSizeRollBackups value="100"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="false"/>
<param name="DatePattern" value="yyyyMM\\yyyyMMdd\\"Service.log""/>
<layout type="log4net.Layout.PatternLayout">
<header value="************ System Start ********** "/>
<footer value="************ System End ************ "/>
<conversionPattern value="%d %-5p [%c] (line:%L) %n%m%n%n"/>
</layout>
</appender>
<appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="./log/Info.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<maxSizeRollBackups value="100"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="false"/>
<param name="DatePattern" value="yyyyMM\\yyyyMMdd\\"Service.Info.log""/>
<layout type="log4net.Layout.PatternLayout">
<header value="************ System Start ********** "/>
<footer value="************ System End ************ "/>
<conversionPattern value="#%d %-5p [%c] (line:%L) %n%m%n%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<appender name="WarnRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="./log/Warn.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<param name="DatePattern" value="yyyyMM\\yyyyMMdd\\"Service.Warn.log""/>
<maxSizeRollBackups value="100"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<header value="************ System Start ********** "/>
<footer value="************ System End ************ "/>
<conversionPattern value="!%d %-5p [%c] (line:%L) %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="WARN"/>
<param name="LevelMax" value="WARN"/>
</filter>
</appender>
<appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="./log/Error.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<param name="DatePattern" value="yyyyMM\\yyyyMMdd\\"Service.Error.log""/>
<maxSizeRollBackups value="100"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<header value="************ System Start ********** "/>
<footer value="************ System End ************ "/>
<conversionPattern value="x!!%d %-5p [%c] (line:%L) %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="InfoRollingFileAppender"/>
<appender-ref ref="WarnRollingFileAppender"/>
<appender-ref ref="ErrorRollingFileAppender"/>
</root>
</log4net>
</configuration>