I'm using the Log4net LoggerMatchFilter to log info only from certain class.
The thing is that the loggerToMatch property is using StartsWith(string)
to compare and as result I get logs also from classes that their name begin with the name of my desired class.
My config contains somthing like that:
<filter type="log4net.Filter.LoggerMatchFilter">
<acceptOnMatch value="true" />
<loggerToMatch value="Class.Name.Space.MyClassName" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
In the log I get also lines from classes like: Class.Name.Space.MyClassName123
Is there a way to filter with "match whole word" condition? (My code is c# .Net 3.5)
You can implement such a Filter
by eg. inheriting from LoggerMatchFilter
so you can reuse some features, while you override the matching algorithm using a full equality comparison instead; loggingEvent.LoggerName == this.LoggerToMatch
.
Borrowing from Log4net's code, this looks like:
namespace PFX.Logging
{
public class LoggerFullMatchFilter : log4net.Filter.LoggerMatchFilter
{
public override FilterDecision Decide(LoggingEvent loggingEvent)
{
if (loggingEvent == null)
{
throw new ArgumentNullException("loggingEvent");
}
if (this.LoggerToMatch != null
&& this.LoggerToMatch.Length != 0
&& loggingEvent.LoggerName == this.LoggerToMatch
)
{
if (this.AcceptOnMatch)
{
return FilterDecision.Accept;
}
return FilterDecision.Deny;
}
return FilterDecision.Neutral;
}
}
}
You use this custom filter in your configuration as shown below, specifying its fully qualified assembly name (namespace.class, assembly)
<filter type="PFX.Logging.LoggerFullMatchFilter, Lib">
<acceptOnMatch value="true" />
<loggerToMatch value="Class.Name.Space.MyClassName" />
</filter>