How to AND log4net filters together

安稳与你 提交于 2019-12-04 03:24:52
Narayan Akhade

You can write a custom AndFilter, which is fairly easy. You can use the code posted here - https://stackoverflow.com/a/8859037/984438

Usage will be like:

<filter type="Namespace.AndFilter, Assembly">
      <!--log only INFO level-->
      <filter type="log4net.Filter.LevelMatchFilter">
          <levelToMatch value="INFO" />
      </filter>

      <!--log only UserController logger-->
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="MyLogger" />
      </filter>
      <acceptOnMatch value="true"/>
</filter>
<!-- do not log anything else -->
<filter type="log4net.Filter.DenyAllFilter" />

You could use ForwardingAppender (see Config examples) and put one filter there and the other in your target appender (or chain them at will).

<appender name="FilterOnlyInfo" type="log4net.Appender.ForwardingAppender">
  <!--log only INFO level-->
  <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="INFO" />
  </filter>
  <appender-ref ref="FinalAppender" />
</appender>
<appender name="FinalAppender">
  <!--log only UserController logger-->
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="MyLogger" />
  </filter>
</appender>

I believe this appender might not have been part of log4net at the time of the question.

Workaround

I just came up with the following workaround that seems to do the trick. First, I removed the loggerToMatch filter from the appender. Then I modified the logger settings as such:

<root>
      <level value="ALL" />
      <appender-ref ref="OtherAppenders" />
</root>

<logger name="MyLogger">
      <level value="INFO" />
      <appender-ref ref="MyAppender" />
</logger>

Since I only have one filter my filter logic is fine. The logger section points to this appender only for my chosen logger, yet all the other appenders pick up the event as well because they're specified in the root. This works but it doesn't explain how to AND the filters together in the appender. I would still like to know.

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