在方法名称上过滤log4net-不太了解 [英] Filtering log4net on method name - can't quite get it

查看:81
本文介绍了在方法名称上过滤log4net-不太了解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用log4net记录我的Web应用程序的进度,并使用Log4PostSharp进行AOP注入所有方法.这具有记录(几乎)所有内容的预期效果,并且很好.

I'm using log4net to log my web app's progress, using Log4PostSharp to AOP-injectify all methods. This has the desired effect of logging (almost) everything and is fine.

我现在需要将JUST Page_Load方法记录到文件/控制台.我显然可以抽筋log4postsharp类来做到这一点,但是那样我将失去所有其他日志记录.

I now have a requirement to log JUST Page_Load methods to a file / console. I can obviously hamstring the log4postsharp class to do that, but then I'd be losing all the other logging.

我一直在看Log4net中的过滤器,从StringMatch过滤器开始,但是仅查看正在记录的消息,而我在方法名后面.这使我进入了PropertyFilter,但仍然不满意.因此,我的log4net.config代码段是:

I've been looking at filters in log4net, starting with the StringMatch filter, but that only looks at the message being logged, and I'm after the method name. This put me onto the PropertyFilter, but still with no joy. My log4net.config snippet is thus:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <filter type="log4net.Filter.PropertyFilter">
    <key value="LocationInfo.MethodName"/>
    <stringToMatch value="Page_Load"/>
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
  <file value="d:\\xxxx\\yyyyy\\zzzzLog"/>

如您所见,我正在尝试通过LocationInfo键入日志记录事件的MethodName,但是我仍然在记录所有内容.如评论中所述,我现在包含了我在RTFM之后添加的DenyAllFilter;-)

As you can see, I'm trying to key into the MethodName of the logging event via LocationInfo, but I'm still getting everything logged. As mentioned in the comments, I've now included the DenyAllFilter that I added after RTFM ;-)

有人可以协助吗?

谢谢

麦克K.

推荐答案

据我所知,log4net不知道属性 LocationInfo.MethodName .我没有使用Log4PostSharp,因此无法确定Log4PostSharp是否会创建此属性.

As far as I can tell log4net does not know a property LocationInfo.MethodName. I am not using Log4PostSharp, so I cannot tell for sure if Log4PostSharp would create this property.

我会这样编写自己的过滤器,以按方法名称过滤(省略正则表达式部分):

I would write my own filter like this to filter by method names (regex part omitted):

public class MethodNameFilter : StringMatchFilter
{       
     override public FilterDecision Decide(LoggingEvent loggingEvent) 
     {
         if (loggingEvent == null)
         {
              throw new ArgumentNullException("loggingEvent");
         }

         var locationInfo = loggingEvent.LocationInformation;

         // Check if we have been setup to filter
         if (locationInfo == null || (m_stringToMatch == null && m_regexToMatch == null))
         {
             // We cannot filter so allow the filter chain
             // to continue processing
              return FilterDecision.Neutral;
         }

         if (m_stringToMatch != null)
         {
             // Check substring match
             if (locationInfo.MethodName.IndexOf(m_stringToMatch) == -1) 
             {
                  // No match, continue processing
                  return FilterDecision.Neutral;
             }

             // we've got a match
             if (m_acceptOnMatch) 
             {
                  return FilterDecision.Accept;
             } 
             return FilterDecision.Deny;
         }
         return FilterDecision.Neutral;
    }
} 

注意:访问LocationInfo是昂贵,并且可能会对性能产生明显影响.您可以考虑修改Log4PostSharp,以在编织过程中提取方法名称并将其存储在某些属性中.在这种情况下,您可以按预期使用属性过滤器,并且不会对性能产生任何影响.不知道这是否真的有效,但我希望这是可能的.

Note: Accessing LocationInfo is expensive and can have a noticeable effect on performance. You could consider to modify Log4PostSharp to extract the method name during weaving and store it in some property. In that case you could use the property filter as you intended and you would not have any impact on performance. Not sure if this really works but I would expect that this is possible.

配置看起来像这样:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
     <filter type="YourNameSpace.MethodNameFilter">
         <stringToMatch value="Page_Load"/>
     </filter>
     <filter type="log4net.Filter.DenyAllFilter" />
     <file value="d:\\xxxx\\yyyyy\\zzzzLog"/>

这篇关于在方法名称上过滤log4net-不太了解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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