Log4Net以编程方式检查IsEnabledFor是否有Appender筛选器 [英] Log4Net programmatically check IsEnabledFor for Appender filters

查看:49
本文介绍了Log4Net以编程方式检查IsEnabledFor是否有Appender筛选器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于特定的附加过滤器,我如何以编程方式检查 IsEnabledFor 是否为 true .

How do I programmatically check that IsEnabledFor is true for a certain appender filter.

这是我的配置:

<log4net>
 <root>
  <level value="ALL" />
  <appender-ref ref="appender" />
 </root>

 <appender name="appender" type="log4net.Appender.FileAppender">
  <filter type="log4net.Filter.LevelRangeFilter">
   <levelMin value="ERROR" />
   <levelMax value="FATAL" />
  </filter>
 </appender>

<log4net>

好,所以如果我将< root> 级别设置为 ERROR 并执行 IsEnabledFor (调试),它将返回正确,但是如果我将< root> 级别设置为 ALL 并将过滤器添加到附加器,则它不考虑过滤器.

Ok so if I set <root> Level to say ERROR and I do a IsEnabledFor(Debug) it returns true but if I set <root> level to ALL and add filters to the appender its not taking into account the filters.

如何获取包含附加过滤器的信息,或者还有另一种查询方式?

How do I get it to include appender filters or is there another way to query this ?

推荐答案

我可能是错的,但是我不知道如何轻松地做到这一点.您可以做的是这样的:

I could be wrong, but I do not see how this can be done easily. What you could do is something like this:

var hierarchy = LogManager.GetRepository() as Hierarchy;
if(hierarchy != null) 
{       
    var appenders = hierarchy.GetAppenders();
    foreach( IAppender appender in appenders)
    {
        var appenderSkeleton = a as AppenderSkeleton
        if (appenderSkeleton != null)
        {
            IFilter filterHead = appenderSkeleton.FilterHead;
            // now analyse the filter chain
        }        
    }
}

我没有测试这段代码,但是它应该或多或少地起作用.通过这种方式,您可以为从 AppenderSkeleton 派生的所有附加程序配置的任何过滤器链的头部.过滤器链的头部实现了 IFilter (与所有过滤器一样),并且它包含一个方法和一个属性:

I did not test this code, but it should work more or less. What you get this way is the head of any configured filter chain for all appenders that derive from AppenderSkeleton. The head of the filter chain implements IFilter (as do all filters) and it contains one method and a property:

FilterDecision Decide(LoggingEvent loggingEvent);   
IFilter Next { get; set; }

现在,您可以使用感兴趣的日志级别创建 LoggingEvent 的实例,并使用 Next 属性遍历过滤器链并测试每个过滤器通过调用 FilterDecision .

Now you could create an instance of a LoggingEvent with the log level you are interested in, and go through the filter chain by using the Next property and test each filter by calling FilterDecision.

这告诉您追加程序是否会接受或拒绝日志记录事件,但是您需要知道,追加程序也可能会基于其他条件(例如消息内容)进行过滤,因此它不一定是"IsEnabledFor"实现.

This tells you if the appender would accept or deny the logging event, but you need to be aware that an appender might also filter based on other criteria (e.g. message content) and thus it would not exactly be an "IsEnabledFor" implementation.

这篇关于Log4Net以编程方式检查IsEnabledFor是否有Appender筛选器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆