从 appender 访问过滤器 属性

access to filter property from the appender

给定一个 class 扩展 FilterSkeleton 一个 属性 名称 MyName 和一个扩展 ApenderSkeleton

是否有可能在 appender 的 Append 方法中获取接受此消息的过滤器的 MyName 属性?

protected override void Append(LoggingEvent loggingEvent)
{
     //sudo
     var somename = acceptedfilter.MyName;

}

可以覆盖FilterEvent方法保存接受消息的过滤器,然后在Append中检索:

public class FilteredAppender : AppenderSkeleton
{
    private IFilter filter;

    protected override bool FilterEvent(LoggingEvent loggingEvent)
    {
        IFilter f = this.FilterHead;

        while (f != null)
        {
            if (f.Decide(loggingEvent) == FilterDecision.Accept)
            {
                filter = f; // Set the filter field
                break;
            }

            f = f.Next;
        }

        return base.FilterEvent(loggingEvent);
    }

    protected override void Append(LoggingEvent loggingEvent)
    {
        NamedFilter acceptedfilter = filter as NamedFilter;            

        if (acceptedfilter!= null)
        {
              var somename = acceptedfilter.MyName;
              // etc
        }
    }
}