I am using log4net in my .NET 3.5 console application and would like the log messages I generate to be seen in both the console standard out and the RollingFileAppender. The file output is working like a charm, but I am seeing a stream of status messages flowing to the console standard out when I execute. I would like to skip all the status information and only see the same messages I am programmatically generating to the log file.
我在我的.NET 3.5控制台应用程序中使用log4net,并希望在控制台标准输出和RollingFileAppender中看到我生成的日志消息。文件输出就像一个魅力,但我看到一个状态消息流在我执行时流向控制台标准。我想跳过所有状态信息,只看到我以编程方式生成日志文件的相同消息。
Here is an example of what I see after I run my app:
以下是我运行应用程序后看到的示例:
log4net: XmlHierarchyConfigurator: Configuration update mode [Merge].
log4net: XmlHierarchyConfigurator: Logger [root] Level string is [DEBUG].
log4net: XmlHierarchyConfigurator: Logger [root] level set to [name="DEBUG",value=30000].
log4net: XmlHierarchyConfigurator: Loading Appender [Console] type: [log4net.Appender.ConsoleAppender]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
and it keeps on going until it describes the whole instantiation of the logger object.
它一直持续到它描述记录器对象的整个实例化为止。
How do I turn this off? Can I? I've tried all sorts of config file settings, but nothing makes these go away! Grrr...
我怎么关掉这个?我可以吗?我已经尝试了各种配置文件设置,但没有什么能让它们消失!哎呀...
9 个解决方案
#1
set debug = false
设置debug = false
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net debug="false">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="your file name" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration>
#2
I just went through this same problem (which, unsurprisingly, is how I found this question).
我刚刚经历了同样的问题(不出所料,我是如何找到这个问题的)。
Anyway, my problem, and possibly yours as well, was caused by a web/app config file setting of "<
log4net debug=true>
". Too obvious, right? I had pasted the skeleton of my app.config settings in from a web snippet, and had focused on the appenders, not really giving the root log4net element a second glance. But there you have it. This is in the FAQ, but again other things caught my eye and not this attribute.
无论如何,我的问题,也可能是你的问题,是由web / app配置文件设置为“
#3
Please try to replicate the problem using a new project one step at the time (first reference log4net with no appender, then with the console appender, then with both appenders). If it shows the same behavior, please post the complete config of log4net.
请尝试一步一步地使用新项目复制问题(首先引用log4net,不使用appender,然后使用控制台appender,然后使用两个appender)。如果它显示相同的行为,请发布log4net的完整配置。
Also you could try using the configuration samples from log4net Config Examples page.
您也可以尝试使用log4net配置示例页面中的配置示例。
Edit: This could be the cause of those messages: How do I enable log4net internal debugging.
编辑:这可能是这些消息的原因:如何启用log4net内部调试。
#4
If you have this in your code:
如果你的代码中有这个:
log4net.Config.BasicConfigurator.Configure();
Change it to:
将其更改为:
log4net.Config.XmlConfigurator.Configure();
#5
Could you provide what your log4net config section looks like, or at least how you have it configured? My best guess is that this answer is correct in that you have log4net internal debugging configured. Either that or you're have the source of log4net in your project and you're compiling it with your own code. That would cause it to pick up your configurations and run it the same way.
你能提供你的log4net配置部分的样子,或者至少你如何配置它?我最好的猜测是,这个答案是正确的,因为你已经配置了log4net内部调试。要么你或者你的项目中有log4net的源代码,你要使用自己的代码进行编译。这将导致它获取您的配置并以相同的方式运行它。
#6
In the app I inherited, there was the line:
在我继承的应用程序中,有一行:
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
Changing that to false
fixed the problem.
将其更改为false可修复问题。
#7
Well, it does say that the root logger level is set to DEBUG. Without access to your configuration file or the code from which the logging is initiated, I would guess that you're implicitly using the default values for the root logger, which
好吧,它确实说根记录器级别设置为DEBUG。如果无法访问配置文件或启动日志记录的代码,我猜你会隐式使用根记录器的默认值,
- Goes to the command line (stdout), and
- is DEBUG by default
转到命令行(stdout),然后
默认情况下是DEBUG
#8
please look at log4net faq. they realy have all those common pitfalls you might encounter.
请看log4net常见问题。他们真的遇到了你可能遇到的所有常见陷阱。
#9
I'm having the same problem as OP. In log4net.config I have set debug = false. I also have a single FileAppender set with a PatternLayout defined. When I usse log.Warn("test") I get the expected formatted results written to the expected txt file. However, I also get a more verbose string written to the console (stdout).
我和OP有同样的问题。在log4net.config中我设置了debug = false。我也有一个FileAppender设置,定义了PatternLayout。当我使用log.Warn(“test”)时,我将预期的格式化结果写入预期的txt文件。但是,我还会将更详细的字符串写入控制台(stdout)。
EDIT: The fix for me was to eliminate this line in my code BasicConfigurator.Configure(); Note that nothing explicitly telling log4net to write to both console and declared FileAppender was stated in my config. In fact, log4net debug=false was declared and the problem continued to persist. The example code on the log4net homepage carelessly invokes BasicConfigurator.Configure(); Although I do use log4net the library suffers from the same problems of many ambitious open source projects. Lengthy XML driven configurations exist to give developers thousands of options for a task that should really have a more streamlined interface available. Programmers don't tend to value each others time. We apply the "don't make me think" rule in our user interfaces but not in our machine interfaces. It's as if we scorn a core principle that is universal to good design. The complexities should be available to the developer, but a few hours shouldn't be lost to accomplish core functionality. Considering this I would say log4net is poorly designed. As with most software there's a lot of complexity for complexity sake. If the developers guiding the project were more talented the most common use case of the library (referencing then using it to log to a text file in the app folder) could be implemented with 5-10 minutes of expose without any previous knowledge. That's not the case because the interface design and the methodology for configuration. It spotlights the reason I hate the way most software engineers think. They are incapable of seeing the exponential value of simplicity for the most common use cases and instead assume the more puzzling they make the interface the more value it will add to other developers. They are driven by ego and ignorance. No wonder most are social outcast.
编辑:我的修复是在我的代码BasicConfigurator.Configure();中消除这一行。请注意,我的配置中没有明确告诉log4net写入控制台和声明的FileAppender。实际上,声明了log4net debug = false并且问题仍然存在。 log4net主页上的示例代码不小心调用BasicConfigurator.Configure();虽然我使用log4net,但是库存在许多雄心勃勃的开源项目的相同问题。存在冗长的XML驱动配置,为开发人员提供了数千个选项,这些选项应该真正具有更简化的可用界面。程序员不倾向于重视彼此的时间。我们在用户界面中应用“不要让我思考”规则,但不在我们的机器界面中应用。就好像我们嘲笑了一个对优秀设计具有普遍性的核心原则。开发人员应该可以使用这些复杂功能,但是不应该丢失几个小时来完成核心功能。考虑到这一点,我会说log4net的设计很差。与大多数软件一样,复杂性也有很多复杂性。如果指导项目的开发人员更有才能,那么库的最常见用例(引用然后使用它来登录app文件夹中的文本文件)可以在没有任何先前知识的情况下暴露5-10分钟来实现。情况并非如此,因为接口设计和配置方法。它突出了我讨厌大多数软件工程师思考方式的原因。对于最常见的用例,他们无法看到简单性的指数值,而是假设他们使界面更加令人费解,它将为其他开发人员增加更多价值。他们受到自我和无知的驱使。难怪大部分都是社会弃儿。
#1
set debug = false
设置debug = false
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net debug="false">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="your file name" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration>
#2
I just went through this same problem (which, unsurprisingly, is how I found this question).
我刚刚经历了同样的问题(不出所料,我是如何找到这个问题的)。
Anyway, my problem, and possibly yours as well, was caused by a web/app config file setting of "<
log4net debug=true>
". Too obvious, right? I had pasted the skeleton of my app.config settings in from a web snippet, and had focused on the appenders, not really giving the root log4net element a second glance. But there you have it. This is in the FAQ, but again other things caught my eye and not this attribute.
无论如何,我的问题,也可能是你的问题,是由web / app配置文件设置为“
#3
Please try to replicate the problem using a new project one step at the time (first reference log4net with no appender, then with the console appender, then with both appenders). If it shows the same behavior, please post the complete config of log4net.
请尝试一步一步地使用新项目复制问题(首先引用log4net,不使用appender,然后使用控制台appender,然后使用两个appender)。如果它显示相同的行为,请发布log4net的完整配置。
Also you could try using the configuration samples from log4net Config Examples page.
您也可以尝试使用log4net配置示例页面中的配置示例。
Edit: This could be the cause of those messages: How do I enable log4net internal debugging.
编辑:这可能是这些消息的原因:如何启用log4net内部调试。
#4
If you have this in your code:
如果你的代码中有这个:
log4net.Config.BasicConfigurator.Configure();
Change it to:
将其更改为:
log4net.Config.XmlConfigurator.Configure();
#5
Could you provide what your log4net config section looks like, or at least how you have it configured? My best guess is that this answer is correct in that you have log4net internal debugging configured. Either that or you're have the source of log4net in your project and you're compiling it with your own code. That would cause it to pick up your configurations and run it the same way.
你能提供你的log4net配置部分的样子,或者至少你如何配置它?我最好的猜测是,这个答案是正确的,因为你已经配置了log4net内部调试。要么你或者你的项目中有log4net的源代码,你要使用自己的代码进行编译。这将导致它获取您的配置并以相同的方式运行它。
#6
In the app I inherited, there was the line:
在我继承的应用程序中,有一行:
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
Changing that to false
fixed the problem.
将其更改为false可修复问题。
#7
Well, it does say that the root logger level is set to DEBUG. Without access to your configuration file or the code from which the logging is initiated, I would guess that you're implicitly using the default values for the root logger, which
好吧,它确实说根记录器级别设置为DEBUG。如果无法访问配置文件或启动日志记录的代码,我猜你会隐式使用根记录器的默认值,
- Goes to the command line (stdout), and
- is DEBUG by default
转到命令行(stdout),然后
默认情况下是DEBUG
#8
please look at log4net faq. they realy have all those common pitfalls you might encounter.
请看log4net常见问题。他们真的遇到了你可能遇到的所有常见陷阱。
#9
I'm having the same problem as OP. In log4net.config I have set debug = false. I also have a single FileAppender set with a PatternLayout defined. When I usse log.Warn("test") I get the expected formatted results written to the expected txt file. However, I also get a more verbose string written to the console (stdout).
我和OP有同样的问题。在log4net.config中我设置了debug = false。我也有一个FileAppender设置,定义了PatternLayout。当我使用log.Warn(“test”)时,我将预期的格式化结果写入预期的txt文件。但是,我还会将更详细的字符串写入控制台(stdout)。
EDIT: The fix for me was to eliminate this line in my code BasicConfigurator.Configure(); Note that nothing explicitly telling log4net to write to both console and declared FileAppender was stated in my config. In fact, log4net debug=false was declared and the problem continued to persist. The example code on the log4net homepage carelessly invokes BasicConfigurator.Configure(); Although I do use log4net the library suffers from the same problems of many ambitious open source projects. Lengthy XML driven configurations exist to give developers thousands of options for a task that should really have a more streamlined interface available. Programmers don't tend to value each others time. We apply the "don't make me think" rule in our user interfaces but not in our machine interfaces. It's as if we scorn a core principle that is universal to good design. The complexities should be available to the developer, but a few hours shouldn't be lost to accomplish core functionality. Considering this I would say log4net is poorly designed. As with most software there's a lot of complexity for complexity sake. If the developers guiding the project were more talented the most common use case of the library (referencing then using it to log to a text file in the app folder) could be implemented with 5-10 minutes of expose without any previous knowledge. That's not the case because the interface design and the methodology for configuration. It spotlights the reason I hate the way most software engineers think. They are incapable of seeing the exponential value of simplicity for the most common use cases and instead assume the more puzzling they make the interface the more value it will add to other developers. They are driven by ego and ignorance. No wonder most are social outcast.
编辑:我的修复是在我的代码BasicConfigurator.Configure();中消除这一行。请注意,我的配置中没有明确告诉log4net写入控制台和声明的FileAppender。实际上,声明了log4net debug = false并且问题仍然存在。 log4net主页上的示例代码不小心调用BasicConfigurator.Configure();虽然我使用log4net,但是库存在许多雄心勃勃的开源项目的相同问题。存在冗长的XML驱动配置,为开发人员提供了数千个选项,这些选项应该真正具有更简化的可用界面。程序员不倾向于重视彼此的时间。我们在用户界面中应用“不要让我思考”规则,但不在我们的机器界面中应用。就好像我们嘲笑了一个对优秀设计具有普遍性的核心原则。开发人员应该可以使用这些复杂功能,但是不应该丢失几个小时来完成核心功能。考虑到这一点,我会说log4net的设计很差。与大多数软件一样,复杂性也有很多复杂性。如果指导项目的开发人员更有才能,那么库的最常见用例(引用然后使用它来登录app文件夹中的文本文件)可以在没有任何先前知识的情况下暴露5-10分钟来实现。情况并非如此,因为接口设计和配置方法。它突出了我讨厌大多数软件工程师思考方式的原因。对于最常见的用例,他们无法看到简单性的指数值,而是假设他们使界面更加令人费解,它将为其他开发人员增加更多价值。他们受到自我和无知的驱使。难怪大部分都是社会弃儿。