Log4net自定义过滤器不过滤

Log4net custom filter not filtering

目前有一个如下所示的 log4net 过滤器。此过滤器不应允许在特定时间间隔内记录相同的消息。但是,消息肯定会被记录下来。资料来源:Log4net, eliminate duplicate messages

public class UniqueLogFilter : FilterSkeleton
{
    private string lastMessage = null;
    private List<Tuple<DateTime, string>> lastMessages = new List<Tuple<DateTime, string>>();

    public int timeWindow { get; set; } = 0;

    public bool lastOnly { get; set; } = false;

    public override FilterDecision Decide(LoggingEvent loggingEvent)
    {
        if (lastOnly)
        {
            if (lastMessage == loggingEvent.RenderedMessage)
            {
                return FilterDecision.Deny;
            }

            lastMessage = loggingEvent.RenderedMessage;
            return FilterDecision.Accept;
        }

        if (timeWindow <= 0)
            return FilterDecision.Accept;

        // Removes old messages
        lastMessages.RemoveAll(m => m.Item1 < DateTime.Now.AddSeconds(0 - timeWindow));

        if (!lastMessages.Any(m => m.Item2 == loggingEvent.RenderedMessage))
        {
            lastMessages.Add(new Tuple<DateTime, string>(loggingEvent.TimeStamp, loggingEvent.RenderedMessage));
            return FilterDecision.Accept;
        }

        return FilterDecision.Deny;
    } 

这是App.Config:

<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
  <file value="Logs\EmailWatcher.log" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5p %d{MM-dd hh:mm:ss} [%thread] %level %logger - %message%newline" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="INFO" />
    <levelMax value="FATAL" />
  </filter>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="Logs\" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5p %d{dd-MM-yyyy hh:mm:ss} [%thread] %level %logger - %message%newline" />
  </layout>
  <datePattern value="'EmailWatcher.'dd.MM.yyyy'.log'" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="5MB" />
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="INFO" />
    <levelMax value="WARN" />
  </filter>
  <filter type="CPVEmailWatcher.UniqueLogFilter">
    <loggerToMatch value="RollingLogFileAppender" />
    <timeWindow value="1800" />
     <!--(30 min interval)-->  
    <lastOnly value="false" />
  </filter>
</appender>
<appender name="ErrorLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="Logs\" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5p%d{ yyyy-MM-dd HH:mm:ss} – [%thread] %m method: %method %n stacktrace: %stacktrace{5} %n type: %type %n line: %line %n" />
  </layout>
  <datePattern value="'EmailWatcher.'dd.MM.yyyy'.log'" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="5MB" />
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="ERROR" />
    <levelMax value="FATAL" />
  </filter>
  <filter type="CPVEmailWatcher.UniqueLogFilter">
    <loggerToMatch value="ErrorLogFileAppender" />
    <timeWindow value="1800" />
     <!--(30 min interval)-->  
    <lastOnly value="false" />
  </filter>
</appender>
<root>
  <level value="INFO" />
  <appender-ref ref="RollingLogFileAppender" />
  <appender-ref ref="ErrorLogFileAppender" />
</root>

UniqueLogFilterDecide 方法不会被执行,因为在过滤器层次结构中,顶部 LevelRangeFilter 接受事件,如果它匹配它的条件而不是将它传递给下一个过滤器,UniqueLogFilter.

为了让 UniqueFilter 决定,您必须在 LevelRangeFilter 上将 AcceptOnMatch 设置为 False
使用此设置,您会注意到 UniqueLogFilter 在调试时被执行。

<acceptOnMatch>False</acceptOnMatch>

过滤器设置如下所示。

<filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="INFO" />
    <levelMax value="WARN" />
    <acceptOnMatch>False</acceptOnMatch>
</filter>
<filter type="CPVEmailWatcher.UniqueLogFilter">
    <loggerToMatch value="RollingLogFileAppender" />
    <timeWindow value="1800" />
    <!--(30 min interval)-->  
    <lastOnly value="false" />
 </filter>