log4net: using StringMatchFilter to filter out unwanted messages
You can use StringMatchFilter in log4net to discard unwanted messages. An example:
<log4net>
<root>
<appender-ref ref="ConsoleAppender"/>
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="message 1"/>
<acceptOnMatch value="false"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="message 2"/>
<acceptOnMatch value="false"/>
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %logger - %m%n" />
</layout>
</appender>
</log4net>
Now in code, do this
log.Error("message 1");
log.Error("message 2");
log.Error("message 3");
Run the codeC:\Temp\log4net\console>dotnet run
2022-03-25 15:02:06,192 [1] ERROR Program - message 3
It did not log message 1 or message 2.
Comments
Post a Comment