配置为忽略日志文件中的现有消息

时间:2018-12-04 05:01:53

标签: c# log4net log4net-configuration log4net-appender log4net-filter

是否可以忽略日志文件中的现有消息。我有这样的配置:

<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>
    </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>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="ErrorLogFileAppender" />
    </root>

是否有任何现有方法可以检查消息是否已经存在,如果不存在则添加,否则忽略。当前的应用程序每隔x分钟运行一次并记录消息,但会使日志文件混乱。

INFO  04-12-2018 03:57:26 [12] INFO App - Connected to mailbox server...
INFO  04-12-2018 03:57:26 [12] INFO App - Looking for folder named 'Working'
INFO  04-12-2018 03:57:27 [12] INFO App - Connected to folder 'Working'
INFO  04-12-2018 03:57:27 [12] INFO App - Total items in the folder 'Working' is: 0
INFO  04-12-2018 03:57:52 [10] INFO App - Connecting to mailbox Office 365
INFO  04-12-2018 03:57:58 [10] INFO App - Connected to mailbox server...
INFO  04-12-2018 03:57:58 [10] INFO App - Looking for folder named 'Working'
INFO  04-12-2018 03:57:59 [10] INFO App - Connected to folder 'Working'
INFO  04-12-2018 03:57:59 [10] INFO App - Total items in the folder 'Working' is: 0

我尝试使用过滤器,但不知道如何进行。谢谢。

1 个答案:

答案 0 :(得分:0)

在这里找到答案。基本上是创建一个自定义过滤器,并与这两个追加程序挂钩。

Log4net, eliminate duplicate messages