Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you filter on Logger level in log4net

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.

like image 524
Rashack Avatar asked Dec 23 '22 12:12

Rashack


1 Answers

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>
like image 54
Peter Lillevold Avatar answered Dec 28 '22 08:12

Peter Lillevold