C# log4net日志分等级打日志

<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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值