How to AND log4net filters together

Alex picture Alex · Dec 28, 2011 · Viewed 12.8k times · Source

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>

Answer

Narayan Akhade picture Narayan Akhade · Jan 15, 2012

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