How to log different log level to different log appender in log4net

百般思念 提交于 2019-12-03 11:01:47
gregwhitaker

You should be able to add a filter to both of your appenders.

<filter type="log4net.Filter.LevelRangeFilter">
         <levelMin value="INFO" />
         <levelMax value="FATAL" />
</filter>

This way one appender will only log to a certain level while the other to a different level even though they are defined by the same logger.

IAmTimCorey

I agree with @gwhitake that you can use the level range filter. I also want to add, however, that you can use the LevelMatch filter if you just want to select one level. This filter allows you to add another filter at the end of it as well so that you could create a filter that had two or more levels even if they aren't next to each other in the order.

For example, the following filter will capture only DEBUG and ERROR messages (just as an example):

<filter type="log4net.Filter.LevelMatchFilter">
  <levelToMatch value="DEBUG"/>
</filter>
<filter type="log4net.Filter.LevelMatchFilter">
  <levelToMatch value="ERROR"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

The one key here is that we need to have the DenyAllFilter at the end. This tells the logger that if it reaches this line, don't log it. That way it will log DEBUG messages, ERROR messages, and nothing else.

There are a lot of ways to manipulate the filter to get exactly what you want. Here is an article (full disclaimer: I wrote it) that shows you how to do a number of different things with the filter besides just level range:

http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!