<appender name="RollingLogFileAppender" ........>
........
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="ERROR" />
</filter>
</appender>
控制级别,由【低】到【高】:ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF,是的,ALL最低,没记错!!
在<appender>最后面加Fileter过滤,通过Filter过滤,只记录等级范围为INFO到ERROR
可以在另一个打日志的方式中写别的等级好区分开两种Log
例如,Log4Mongo:
<appender name='MongoDBAppender' ........>
........
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="Debug" />
<levelMax value="Debug" />
</filter>
</appender>
通过Filter过滤,只记录等级Debug
最后的root写法如下:
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref='MongoDBAppender' />
</root>
例【log4net.config】:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- This section contains the log4net configuration settings -->
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="Log/" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<staticLogFileName value="false" />
<datePattern value="yyyyMMdd-HH'.log'" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %-4logger %message%newline" />
</layout>
</appender>
<appender name="MongoDBAppender" type="Log4Mongo.MongoDBAppender, Log4Mongo">
<connectionString value="mongodb://root:root@127.0.0.1:27017" />
<CollectionName value="yyyyMMdd"/>
<CollectionPreName value="mongodb"/>
<recordingCycle value="1"/>
<field>
<name value="timestamp" />
<layout type="log4net.Layout.PatternLayout" value="%date" />
</field>
<field>
<name value="thread" />
<layout type="log4net.Layout.PatternLayout" value="%thread" />
</field>
<field>
<name value="level" />
<layout type="log4net.Layout.PatternLayout" value="%level" />
</field>
<field>
<name value="logger" />
<layout type="log4net.Layout.PatternLayout" value="%logger" />
</field>
<field>
<name value="message" />
<layout type="log4net.Layout.PatternLayout" value="%message" />
</field>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="ERROR" />
</filter>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<!--控制级别,由低到高:ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref='MongoDBAppender' />
</root>
</log4net>
</configuration>