I am struggling to write an AND conditional filter in log4net. Had it been nLog, I could have written it this way:
&l
A custom filter supporting AND conditions. This class exposes Filter property so existing log4net filters can be used here and also one can have nested AND conditions if required.
public class AndFilter : FilterSkeleton
{
private bool acceptOnMatch;
private readonly IList filters = new List();
public override FilterDecision Decide(LoggingEvent loggingEvent)
{
if (loggingEvent == null)
throw new ArgumentNullException("loggingEvent");
foreach(IFilter filter in filters)
{
if (filter.Decide(loggingEvent) != FilterDecision.Accept)
return FilterDecision.Neutral; // one of the filter has failed
}
// All conditions are true
if(acceptOnMatch)
return FilterDecision.Accept;
else
return FilterDecision.Deny;
}
public IFilter Filter
{
set { filters.Add(value); }
}
public bool AcceptOnMatch
{
get { return acceptOnMatch;}
set { acceptOnMatch = value;}
}
}
Config: