针对特定异常类型的Log4Net appender过滤器?

Dyl*_*rry 7 log4net

我正在尝试根据记录的异常类型过滤我的appender.这可能在log4net中吗?

Ste*_*gli 10

log4net不直接支持此功能.但是,您可以通过从IFilter接口或FilterSkeleton类(在log4net.Filter命名空间中)派生来轻松实现自己的过滤器.

像这样的东西应该做的伎俩:

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

然后,您可以像常规过滤器一样使用此过滤器.有关进一步参考,您可以查看标准log4net过滤器的源代码.