Search code examples
logginglog4neterror-logging

log4net - how to filter out exceptions which contain a specified text?


I use log4net to log exceptions, and it generally works fine. But I would like to be able to filter out exceptions which contain a specific text string.

I found this example of implementing a filter, but it does the opposite of what I want; it only includes messages with the specified string. I want to exclude the specified string.

<filter type="log4net.Filter.StringMatchFilter">
  <stringToMatch value="My Exclude String" />
</filter>

<filter type="log4net.Filter.DenyAllFilter" />

Is it possible to exclude the specified string instead?


Solution

  • Just invert your filter in the configuration by using the acceptOnMatch property on the filter.

    <filter type="log4net.Filter.StringMatchFilter">
      <stringToMatch value="My Exclude String" />
      <acceptOnMatch value="false" />
    </filter>