log4net按照不同的级别输出到不同文件

 

配置目标:

1)一个文件记录全部级别的日志;

2)记录某一级别的日志(使用LevelRangeFilter进行级别的过滤)。

 

Eg:

  1. <log4net>
  2.   <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  3.     <layout type="log4net.Layout.PatternLayout">
  4.       <conversionPattern value="%d [%t] %-5p %l - %m%n %property{ServiceType}"/>
  5.     </layout>
  6.   </appender>
  7.   <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
  8.     <layout type="log4net.Layout.PatternLayout">
  9.       <conversionPattern value="%d [%t] %-5p %l - %m%n"/>
  10.     </layout>
  11.   </appender>
  12.   <appender name="RollingFileAppenderAll" type="log4net.Appender.RollingFileAppender">
  13.     <param name="File" value="Log/alllLog.txt"/>
  14.     <appendToFile value="true"/> <rollingStyle value="Composite"/>
  15.     <datePattern value="yyyyMM"/>
  16.     <maxSizeRollBackups value="100"/>
  17.     <maximumFileSize value="2MB"/>
  18.  
  19.     <PreserveLogFileNameExtension value="true"/>
  20.     <staticLogFileName value="false"/>
  21.     <layout type="log4net.Layout.PatternLayout">
  22.       <param name="ConversionPattern" value="%d %-5p %m%n"/>
  23.     </layout>
  24.   </appender>
  25.  
  26.   <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender" LEVEL="ERROR">
  27.     <file value="Log/Error.txt" />
  28.     <appendToFile value="true" />
  29.     <rollingStyle value="Size" />
  30.     <maxSizeRollBackups value="10" />
  31.     <maximumFileSize value="1024KB" />
  32.     <staticLogFileName value="true" />
  33.     <layout type="log4net.Layout.PatternLayout">
  34.       <conversionPattern value="%date [%thread] %-5level [%logger] [%property{NDC}] - %message%newline" />
  35.     </layout>
  36.     <filter type="log4net.Filter.LevelRangeFilter">
  37.       <param name="LevelMin" value="ERROR" />
  38.       <param name="LevelMax" value="ERROR" />
  39.     </filter>
  40.   </appender>
  41.  
  42.   <root>
  43.     <level value="DEBUG"/>
  44.     <appender-ref ref="ConsoleAppender"/>
  45.     <appender-ref ref="RollingFileAppenderAll"/>
  46.     <appender-ref ref="ErrorRollingFileAppender"/>
  47.   </root>
  48. </log4net>
posted @ 2015-05-26 11:36  _DN  阅读(493)  评论(0编辑  收藏  举报