正文:
这是用log4net 1.2.9.0环境下 将不同等级消息输出到不同日志文件的配置文件,log4net.config
在项目中增加引用,在AssemblyInfo.cs里增加一句话:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
在Application_OnStart-WebForm或者在 main函数-WinForm里面增加
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<!--
ALL/FATAL > ERROR > WARN > INFO > DEBUG/OFF
<log4net>
<!--
ALL/FATAL > ERROR > WARN > INFO > DEBUG/OFF
<logger name="PenavicoxmLogger">
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</logger>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Penavicoxm.log.txt" />
<param name="RollingStyle" value="Date" />
<param name="datePattern" value="MM-dd HH:mm" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="DebugLog" />
</root>-->
<root>
<level value="ALL" />
<appender-ref ref="DebugLog" />
<appender-ref ref="InfoLog" />
<appender-ref ref="WarnLog" />
<appender-ref ref="ErrorLog" />
<appender-ref ref="FatalLog" />
</root>
<!--
<root>
<level value="DEBUG" />
<appender-ref ref="DebugLog" />
</root>
<root>
<level value="FATAL" />
<appender-ref ref="FatalLog" />
</root>
<root>
<level value="WARN" />
<appender-ref ref="WarnLog" />
</root>
<root>
<level value="ERROR" />
<appender-ref ref="ErrorLog" />
</root>-->
<appender name="InfoLog" type="log4net.Appender.RollingFileAppender">
<file value="log\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy\\MM\\dd-In\fo'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c %L - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</logger>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Penavicoxm.log.txt" />
<param name="RollingStyle" value="Date" />
<param name="datePattern" value="MM-dd HH:mm" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="DebugLog" />
</root>-->
<root>
<level value="ALL" />
<appender-ref ref="DebugLog" />
<appender-ref ref="InfoLog" />
<appender-ref ref="WarnLog" />
<appender-ref ref="ErrorLog" />
<appender-ref ref="FatalLog" />
</root>
<!--
<root>
<level value="DEBUG" />
<appender-ref ref="DebugLog" />
</root>
<root>
<level value="FATAL" />
<appender-ref ref="FatalLog" />
</root>
<root>
<level value="WARN" />
<appender-ref ref="WarnLog" />
</root>
<root>
<level value="ERROR" />
<appender-ref ref="ErrorLog" />
</root>-->
<appender name="InfoLog" type="log4net.Appender.RollingFileAppender">
<file value="log\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy\\MM\\dd-In\fo'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c %L - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<appender name="DebugLog" type="log4net.Appender.RollingFileAppender">
<file value="log\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy\\MM\\dd-Debu\g'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c %L - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<appender name="FatalLog" type="log4net.Appender.RollingFileAppender">
<file value="log\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy\\MM\\dd-\Fa\tal'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c %L - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="FATAL" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<appender name="WarnLog" type="log4net.Appender.RollingFileAppender">
<file value="log\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy\\MM\\dd-Warn'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c %L - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
<file value="log\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy\\MM\\dd-Error'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c %L - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
</log4net>
<appender name="DebugLog" type="log4net.Appender.RollingFileAppender">
<file value="log\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy\\MM\\dd-Debu\g'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c %L - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<appender name="FatalLog" type="log4net.Appender.RollingFileAppender">
<file value="log\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy\\MM\\dd-\Fa\tal'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c %L - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="FATAL" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<appender name="WarnLog" type="log4net.Appender.RollingFileAppender">
<file value="log\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy\\MM\\dd-Warn'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c %L - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
<file value="log\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy\\MM\\dd-Error'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c %L - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
</log4net>
刚用log4net,也许配置比较累赘,主要使用filter的, 高手指点一下,有没有简洁的方式配置
类别: C#.Net
发布日期: 2007-8-12 1:47