如何消除log4net中的重复日志记录? [英] How do I eliminate duplicate logging in log4net?
问题描述
我有一个程序,可以对"myprogram"记录器进行许多log4net调用.它还调用其他使log4net调用其他记录器的代码.我想为"myprogram"捕获高于INFO的所有日志,并为其他所有捕获高于WARN的所有日志.通过这种方式,我可以获取特定于我正在处理的任务的正在进行中的消息,但仍会在支持代码中收到有关潜在不良情况的通知.我希望将其发送到控制台和日志文件.
I have a program that makes many log4net calls to the "myprogram" loggers. It also calls other code that makes log4net calls to other loggers. I want to capture all logs higher than INFO for "myprogram" and all logs higher than WARN for everything else. This way, I get the work-in-progress messages specific to the task I'm working on, but am still notified of potentially bad things happening in the supporting code. I want this sent to both Console and a log file.
我有以下log4net配置:
I have the following log4net config:
<log4net>
<root>
<level value="WARN" />
<appender-ref ref="Console" />
<appender-ref ref="LogFile" />
</root>
<logger name="myprogram">
<level value="INFO" />
<appender-ref ref="Console" />
<appender-ref ref="LogFile" />
</logger>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
<threshold value="INFO" />
</appender>
<appender name="LogFile" type="log4net.Appender.RollingFileAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="- %utcdate %level %logger %ndc %thread %message%newline" />
</layout>
<appendToFile value="false" />
<staticLogFileName value="true" />
<rollingStyle value="Once" />
<file value="mylogfile" />
<immediateFlush value="true" />
<threshold value="INFO" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
</log4net>
这对我来说非常有意义:对所有内容登录> WARN,对特定的"myprogram"记录器登录> INFO.
This makes perfect sense to me: log >WARN for everything and >INFO for the specific "myprogram" logger.
问题是我在Console和LogFile上都记录了两次的INFO消息.仅当我同时填充了<root>
和<logger>
元素时,才会发生这种情况.如果我删除其中任何一个,则其余的按我的预期工作.
The problem is that I'm getting INFO messages logged twice on both Console and LogFile. This only happens if I have both the <root>
and <logger>
elements filled though; if I remove either one, then the remaining one works as I expect.
我能理解我是否正在两次记录WARN条目(因为myprogram同时匹配"root"和"myprogram"),但是即使将ROOT(大概)设置为WARN,它也在INFO发生.
I could understand if I was getting double-logging of WARN entries (since myprogram matches both "root" and "myprogram"), but it's happening at INFO even though ROOT is (presumably) set to WARN.
我在这里做错什么了吗,或者这是一个log4net错误/歧义?
Am I doing something wrong here, or is this a log4net bug/ambiguity?
推荐答案
您正在重复,因为您告诉它两次记录消息.我不建议在这里使用可加性,因为您可能会遇到一些副作用,只需删除不必要的配置即可:
You are getting duplicated because you are telling it to log messages twice. I wouldn't recommend using additivity here since you might experience some side effects, simply remove unnecessary configuration:
<root>
<level value="WARN" />
<appender-ref ref="Console" />
<appender-ref ref="LogFile" />
</root>
<logger name="myprogram">
<level value="INFO" />
</logger>
您不需要在logger myprogram中指示appender-ref,因为它将从根logger继承它们.如果再次指定它们,它将记录两次.
You don't need to indicate the appender-ref in the logger myprogram since it will inherit them from the root logger; if you specify them again it will log twice.
这篇关于如何消除log4net中的重复日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!