从appender访问过滤器属性

时间:2015-12-07 19:43:08

标签: c# log4net log4net-appender log4net-filter

给定一个类,使用属性名 MyName 扩展 FilterSkeleton ,并扩展 ApenderSkeleton

的appender

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

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

}

1 个答案:

答案 0 :(得分:0)

您可以覆盖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
        }
    }
}