C Morgan
C Morgan

Reputation:

How do I disable log4net status messages to the console?

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.

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...

Upvotes: 13

Views: 13872

Answers (9)

James Curran
James Curran

Reputation: 103505

In the app I inherited, there was the line:

<appSettings>
   <add key="log4net.Internal.Debug" value="true"/>

Changing that to false fixed the problem.

Upvotes: 2

Lukas S.
Lukas S.

Reputation: 5758

If you have this in your code:

log4net.Config.BasicConfigurator.Configure();

Change it to:

log4net.Config.XmlConfigurator.Configure();

Upvotes: 5

user2695105
user2695105

Reputation: 51

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).

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.

Upvotes: 1

Ash
Ash

Reputation: 2601

set 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>

Upvotes: 19

JustinB
JustinB

Reputation: 793

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.

Upvotes: 15

Agent_9191
Agent_9191

Reputation: 7253

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.

Upvotes: 1

Orentet
Orentet

Reputation: 2363

please look at log4net faq. they realy have all those common pitfalls you might encounter.

Upvotes: 0

Steen
Steen

Reputation: 6849

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

  1. Goes to the command line (stdout), and
  2. is DEBUG by default

Upvotes: 0

alexandrul
alexandrul

Reputation: 13246

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.

Also you could try using the configuration samples from log4net Config Examples page.

Edit: This could be the cause of those messages: How do I enable log4net internal debugging.

Upvotes: 3

Related Questions