views:

574

answers:

1

I am want to filter out messages that contain specified string. I used log4net.Filter.StringMatchFilter but that applies to appenders only.

I'd like to filter that message on Logger level already. Something along these lines:

<logger name="MyLogger.WebServices">
 <level value="Debug" />
 <filter type="log4net.Filter.StringMatchFilter">
  <stringToMatch value="do not log me" />
  <acceptOnMatch value="false" />
 </filter>
 <appender-ref ref="file" />
 <appender-ref ref="debug" />
</logger>

Is it actually possible this way? Or is there a different way to handle this situation.

+3  A: 

Filters are for use with appenders only. If you need to filter out events for several appenders you can pre-filter using a forwardingappender. This way you avoid having to copy and paste the same filter on several appenders.

Your example would look like this:

<appender name="screeningAppender" type="log4net.Appenders.ForwardingAppender">
    <filter type="log4net.Filter.StringMatchFilter">
            <stringToMatch value="do not log me" />
            <acceptOnMatch value="false" />
    </filter>

    <appender-ref ref="file" />
    <appender-ref ref="debug" />
</appender>

<logger name="MyLogger.WebServices">
    <level value="Debug" />
    <appender-ref ref="screeningAppender" />
</logger>
Peter Lillevold
That's what I feared. Cheers!
Rashack
I had thought to make a custom appender or derive from one that was environment aware (e.g. dev or production), but I like the idea of moving this to a filter to "give" all appenders that capability.
Kit