How to AND log4net filters together
Asked Answered
W

3

9

I would like to create an appender that logs only for a particular level AND only for a particular logger. From what I'm seeing, and based on this tutorial, the filters are ORed together. How can I AND the log4net filters together? Here's an example of what I'm doing:

<appender name="MyAppender">
      <!--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>

      <!-- do not log anything else -->
      <filter type="log4net.Filter.DenyAllFilter" />
</appender>
Whomsoever answered 27/12, 2011 at 23:41 Comment(1)
possible duplicate of Log4Net filters "OR"Aleut
E
9

You can write a custom AndFilter, which is fairly easy. You can use the code posted here - https://mcmap.net/q/93957/-log4net-filter-how-to-write-and-filter-to-ignore-log-messages

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" />
Eyebright answered 15/1, 2012 at 0:53 Comment(1)
Doesn't sound too bad, but I'm surprised such functionality is not built into log4netWhomsoever
A
8

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.

Adne answered 18/5, 2015 at 7:43 Comment(0)
W
2

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.

Whomsoever answered 28/12, 2011 at 0:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.