gpt4 book ai didi

c# - 自定义 Log4Net 过滤器级别不写入日志

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:00 24 4
gpt4 key购买 nike

我已经尝试过但没有成功:How do I filter on a custom level in log4net

这是我使用的代码:

public static class SpecialLogging
{
public static readonly log4net.Core.Level specialLevel = new log4net.Core.Level(35000, "SPECIAL");

public static void Special(this ILog log, string message)
{
log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType,
specialLevel, message, null);
}
}

这是我的测试:

private static void Main()
{
//Logger
LogManager.GetRepository().LevelMap.Add(SpecialLogging.specialLevel);
ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Special("Special");
}

最后是我的 log4net.config 文件:

<log4net>
<appender name="RollingLogFileAppenderDebug" type="log4net.Appender.RollingFileAppender">
<file value="./Logs/DEBUG.log" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="3" />
<maximumFileSize value="5GB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>

<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{G} [%thread] (%stacktrace) %5level - %message%newline" />
</layout>
</appender>

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="./Logs/Special.log" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="3" />
<maximumFileSize value="5GB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

<filter type="log4net.Filter.LevelMatchFilter">
<acceptOnMatch value="true" />
<levelToMatch value="SPECIAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{G} [%thread] (%stacktrace) %5level - %message%newline" />
</layout>
</appender>

<!-- Set the default logging level and add the active appenders -->
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="RollingLogFileAppenderDebug" />
</root>

</log4net>

特殊信息出现在Debug.log文件中,但是Special.log文件是空的!

最佳答案

您错过了 Olle's answer来自您链接到的问题。

所有 log4net 问题的首要规则是启用 internal debugging .

在这种情况下,如果你添加

log4net.Util.LogLog.InternalDebugging = true;

到你的主程序的开始,然后你可能会看到错误

log4net:ERROR XmlHierarchyConfigurator: Unknown Level Specified [SPECIAL]
log4net:WARN Unable to set property [levelToMatch] on object [log4net.Filter.LevelMatchFilter] using value [SPECIAL] (with acceptable conversion types)

所以解决方案是将此信息添加到您的 log4net 配置部分,即

<level>
<name value="SPECIAL" />
<value value="35000" />
</level>

关于c# - 自定义 Log4Net 过滤器级别不写入日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12484179/

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