jdylanmc
jdylanmc

Reputation: 859

Log4Net appender filter for specific exception type?

I'm trying to filter my appender based on the type of exception being logged. Is this possible in log4net?

Upvotes: 7

Views: 1437

Answers (1)

Stefan Egli
Stefan Egli

Reputation: 17018

log4net does not support this directly. You can however quite easily implement your own filter by deriving either from the IFilter interface or the FilterSkeleton class (both in log4net.Filter namespace).

Something like this should do the trick:

public class ExceptionTypeFilter : FilterSkeleton
{
     override public FilterDecision Decide(LoggingEvent loggingEvent)
     { 
          var ex = loggingEvent.ExceptionObject as YourExceptionType;
          return (ex != null) ? FilterDecision.Accept : FilterDecision.Deny;         
     }
}

This filter you can then use like a regular filter. For further reference you may look at the source code of the standard log4net filters.

Upvotes: 10

Related Questions