gpt4 book ai didi

c# - Log4net自定义过滤器不过滤

转载 作者:行者123 更新时间:2023-11-30 15:14:52 24 4
gpt4 key购买 nike

目前有一个如下所示的 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>

关于c# - Log4net自定义过滤器不过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53643591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com