C# 如何消除 log4net 中的重复登录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/651277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I eliminate duplicate logging in log4net?
提问by Craig Walker
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.
我有一个程序可以对“myprogram”记录器进行多次 log4net 调用。它还调用其他代码,使 log4net 调用其他记录器。我想为“myprogram”捕获所有高于 INFO 的日志,并为其他所有记录捕获高于 WARN 的所有日志。通过这种方式,我可以获得特定于我正在处理的任务的正在进行的工作消息,但仍会收到有关支持代码中发生的潜在坏事的通知。我希望将其发送到控制台和日志文件。
I have the following log4net config:
我有以下 log4net 配置:
<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>
This makes perfect sense to me: log >WARN for everything and >INFO for the specific "myprogram" logger.
这对我来说非常有意义:记录所有内容的 >WARN 和特定“myprogram”记录器的 >INFO。
The problem is that I'm getting INFO messages logged twiceon 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.
问题是我在Console 和 LogFile 上都记录了两次INFO 消息。这只有在我同时填充<root>
和<logger>
元素时才会发生;如果我删除任何一个,那么剩下的一个会按我的预期工作。
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.
我可以理解我是否获得了 WARN 条目的双重记录(因为 myprogram 与“root”和“myprogram”都匹配),但是即使 ROOT(大概)设置为 WARN,它也会在 INFO 中发生。
Am I doing something wrong here, or is this a log4net bug/ambiguity?
我在这里做错了什么,还是这是一个 log4net 错误/歧义?
采纳答案by Eddie
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>
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.
您不需要在记录器 myprogram 中指明 appender-ref,因为它将从根记录器继承它们;如果您再次指定它们,它将记录两次。
回答by Eddie
Try with this change, setting additivity to false.
尝试进行此更改,将可加性设置为 false。
<root>
<level value="WARN" />
<appender-ref ref="Console" />
<appender-ref ref="LogFile" />
</root>
<logger name="myprogram" additivity="false">
<level value="INFO" />
<appender-ref ref="Console" />
<appender-ref ref="LogFile" />
</logger>