我复制了log4net示例以实现自定义日志级别AUDIT。我将AUDIT定义为值为35000,介于DEBUG 30000和INFO 40000之间。
以下是我的配置部分。我有一个记录所有级别的appender和一个只记录AUDIT级别的appender(这仅用于测试)
<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");
}
然而,appender的输出是相同的......
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]
答案 0 :(得分:20)
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>
答案 1 :(得分:6)
您需要添加
<level>
<name value="AUDIT" />
<value value="35000" />
</level>