log4net filter - how to write AND filter to ignore log messages

前端 未结 2 746
粉色の甜心
粉色の甜心 2020-12-06 01:44

I am struggling to write an AND conditional filter in log4net. Had it been nLog, I could have written it this way:

&l         


        
2条回答
  •  孤街浪徒
    2020-12-06 01:58

    You can create custom filter for your business needs:

    public class UserRequestFilter : FilterSkeleton
    {
        public override FilterDecision Decide(LoggingEvent loggingEvent)
        {
            if (loggingEvent == null)
                throw new ArgumentNullException("loggingEvent");
    
            string userId = (string)loggingEvent.Properties["UserId"];
            string url = (string)loggingEvent.Properties["Url"];
    
            if (String.IsNullOrEmpty(UserId) || String.IsNullOrEmpty(Url))
                return FilterDecision.Neutral;
    
            if (UserId.Equals(userId) && Url.Equals(url, StringComparison.CurrentCultureIgnoreCase))
                return AcceptOnMatch ? FilterDecision.Accept : FilterDecision.Deny;
    
            return FilterDecision.Neutral;
        }
    
        public bool AcceptOnMatch { get; set; }
        public string UserId { get; set; }
        public string Url { get; set; }
    }
    

    Configuration will look like this:

    
      
      
      
    
    

    Also you can create compound filter, but I didn't find way to use it in configuration. Looks like it could be attached only programmatically (which is useless ^_^):

    IAppenderAttachable logger = (IAppenderAttachable)_log.Logger;
    AppenderSkeleton appender = (AppenderSkeleton)logger.GetAppender("appenderName");
    CompoundFilter compoundFilter = new CompoundFilter();
    compoundFilter.AddFilter(new PropertyFilter() { Key = "UserId", StringToMatch = "TEST" });
    compoundFilter.AddFilter(new PropertyFilter() { Key = "Url", StringToMatch = @"/foo/foobar.aspx" });
    appender.AddFilter(compoundFilter);
    logger.AddAppender(appender);
    

    [UPDATE]

    Here is trick that you can use - create filter, which will check all filters down the filters chain:

    public class DenyAllSubsequentFilter : FilterSkeleton
    {
        public override FilterDecision Decide(LoggingEvent loggingEvent)
        {
            IFilter nextFilter = Next;
            if (nextFilter == null)
                return FilterDecision.Accept;
    
            while (nextFilter != null)
            {
                if (nextFilter.Decide(loggingEvent) != FilterDecision.Deny)
                    return FilterDecision.Accept;
    
                nextFilter = nextFilter.Next;
            }
    
            return FilterDecision.Deny;
        }
    }
    

    Usage:

    
    
      
      
      
    
    
      
      
      
    
    

    This filter will deny logging message if all subsequent filters will deny it.

提交回复
热议问题