Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net appender filter for specific exception type?

Tags:

log4net

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

like image 325
Dylan McCurry Avatar asked Jul 05 '12 21:07

Dylan McCurry


1 Answers

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.

like image 145
Stefan Egli Avatar answered Sep 22 '22 03:09

Stefan Egli