Log4net 根据日志类别保存到不同的文件,并按照日期生成不同文件名称

配置文件:

 1 <configuration>
 2     <configSections>
 3         <!--日志记录-->
 4         <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
 5     </configSections>
 6 
 7     <log4net>
 8         <root></root>
 9         <logger name="Logger">
10             <level value="ALL" />
11             <appeNder-ref ref="ErrorRollingFileAppender" />
12             <appender-ref ref="InfoRollingFileAppender" />
13         </logger>
14         
15         <!--数据日志-->
16         <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
17             <param name="File" value="Log\" />
18             <param name="AppendToFile" value="true" />
19             <param name="rollingStyle" value="Date" />
20             <param name="datePattern" value="yyyy-MM-dd.'Info.txt'" />
21             <param name="staticLogFileName" value="false"/>
22             <layout type="log4net.Layout.PatternLayout">
23                 <param name="ConversionPattern" value="%d [%t] %-5p %c [%x]  - %m%n"/>
24             </layout>
25             <filter type="log4net.Filter.LevelRangeFilter">
26                 <param name="LevelMin" value="INFO" />
27                 <param name="LevelMax" value="INFO" />
28             </filter>
29         </appender>
30 
31         <!--异常日志-->
32         <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
33             <param name="File" value="Log\" />
34             <param name="AppendToFile" value="true" />
35             <param name="rollingStyle" value="Date" />
36             <param name="datePattern" value="yyyy-MM-dd.'Error.txt'" />
37             <param name="staticLogFileName" value="false"/>
38             <layout type="log4net.Layout.PatternLayout">
39                 <param name="ConversionPattern" value="%d [%t] %-5p %c [%x]  - %m%n"/>
40             </layout>
41             <filter type="log4net.Filter.LevelRangeFilter">
42                 <param name="LevelMin" value="ERROR" />
43                 <param name="LevelMax" value="ERROR" />
44             </filter>
45         </appender>
46     </log4net>
47 </configuration>

 

代码使用记录器:

1  private static readonly ILog infoLogger = LogManager.GetLogger("Logger");
2  XmlConfigurator.Configure();
3  infoLogger.Info(message);

posted on 2012-06-13 20:39  backslash112  阅读(3970)  评论(0编辑  收藏  举报

导航