Log4net filtering by method name - cannot get it

I use log4net to register progress in a web application, using Log4PostSharp for AOP injection of all methods. This has the desired effect of registering (almost) everything and perfectly.

Now I have a requirement to write JUST Page_Load methods to a file / console. I can obviously block the log4postsharp class, but then I will lose all other entries.

I look at the filters in log4net, starting with the StringMatch filter, but this only looks at the message that is being logged, and I'm behind the method name. It put me on a PropertyFilter, but still without joy. So my log4net.config fragment is:

<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"/> 

As you can see, I am trying to enter registration events through MethodIname through LocationInfo, but I still register everything. EDIT: As mentioned in the comments, now I have included DenyAllFilter, which I added after RTFM; -)

Can anyone help?

Thanks,

Mike K.

+7
log4net postsharp log4postsharp
source share
1 answer

As far as I can tell, log4net does not know the properties LocationInfo.MethodName. I do not use Log4PostSharp, so I can’t say for sure whether Log4PostSharp will create this property.

I would write my own filter like this to filter by method names (the regex part is 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; } } 

Note. Access to LocationInfo is expensive and can have a significant impact on performance. You might consider changing Log4PostSharp to retrieve the method name during weaving and store it in some property. In this case, you can use the property filter as you wish, and you would not affect performance. Not sure if this really works, but I expect it to be possible.

The configuration will look something like this:

 <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"/> 
+4
source share

All Articles