gpt4 book ai didi

.net - 如何在 log4net 中过滤自定义级别?

转载 作者:行者123 更新时间:2023-12-04 01:34:29 24 4
gpt4 key购买 nike

我复制了 log4net 示例以实现自定义日志级别 AUDIT。我将 AUDIT 的值定义为 35000,介于 DEBUG 30000 和 INFO 40000 之间。

以下是我的配置部分。我有一个记录所有级别的附加程序,一个应该只记录审计级别(这仅用于测试)

<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender" >
<file value="testrun.log" />
<layout type="log4net.Layout.PatternLayout" >
<conversionPattern value="%date{HH:mm:ss:fff} [%thread] %-5level %logger{1} - %message%newline" />
</layout>
</appender>
<appender name="FileAppender.Audit" type="log4net.Appender.FileAppender" >
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="AUDIT" />
</filter>
<file value="testrun.audit.log" />
<layout type="log4net.Layout.PatternLayout" >
<conversionPattern value="%date{HH:mm:ss:fff} [%thread] %-5level %logger{1} - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="FileAppender" />
<appender-ref ref="FileAppender.Audit" />
</root>
</log4net>

测试方法是
private static readonly IAuditLog log = AuditLogManager.GetLogger(typeof(Program));
public static void Main()
{
log.Debug("Debug");
log.Audit("Audit");
log.Info("Info");
}

但是,附加程序的输出是相同的......
13:09:11:540 [9] DEBUG Program - Debug
13:09:11:560 [9] AUDIT Program - Audit
13:09:11:560 [9] INFO Program - Info

解析 LevelMatchFilter 时的 log4net 调试输出是
log4net: Loading Appender [FileAppender.Audit] type: [log4net.Appender.FileAppender]
log4net:ERROR XmlHierarchyConfigurator: Unknown Level Specified [AUDIT]
log4net:WARN Unable to set property [levelToMatch] on object [log4net.Filter.LevelMatchFilter] using value [AUDIT] (with acceptable conversion types)
log4net: Setting Collection Property [AddFilter] to object [log4net.Filter.LevelMatchFilter]

最佳答案

log4net 版本 1.2.10 要求您结束 LevelMatchFilter链带 DenyAllFilter如果你想要我在问题中描述的行为。

<appender name="FileAppender.Audit" type="log4net.Appender.FileAppender" >
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="AUDIT" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<file value="testrun.audit.log" />
<layout type="log4net.Layout.PatternLayout" >
<conversionPattern value="%date{HH:mm:ss:fff} [%thread] %-5level %logger{1} - %message%newline" />
</layout>
</appender>

关于.net - 如何在 log4net 中过滤自定义级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/538095/

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