Tom Whitaker
Tom Whitaker

Reputation: 53

Log4Net log showing up in console window

I have the opposite problem as most people do with Log4net. I have a basic rolling log appender, I use the INFO, WARN and ERROR levels in my code behind. I have debug turned off in the app.config. It STILL pushes the log data to the console window in addition to the rolling log appender.

How do I stop this behavior? It makes looking at the console window a nightmare.

My config:

<log4net debug="false">
  <appender name="RollingFileAppender"
            type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString"
          value="milliondollars.log"/>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <rollingStyle value="Composite"/>
    <filter type="log4net.Filter.LevelRangeFilter">
      <acceptOnMatch value="true" />
      <levelMin value="INFO" />
      <levelMax value="FATAL" />
    </filter>
    <datePattern value="yyyyMMdd"/>
    <maxSizeRollBackups value="100"/>
    <maximumFileSize value="15MB"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %-5level %logger: %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="RollingFileAppender" />
  </root>
</log4net>

Upvotes: 5

Views: 7009

Answers (3)

Paul C
Paul C

Reputation: 4776

My problem was using this when creating an instance of the logger;

        log4net.Config.BasicConfigurator.Configure();
        ILog log = log4net.LogManager.GetLogger(typeof(Program));

I took me a little while to figure out, this was ignoring my configuration and initializing with defaults instead. Replaced it with this as people suggest and it works fine;

    private static readonly log4net.ILog log = log4net.LogManager.GetLogger
   (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Source: http://elegantcode.com/2007/12/07/getting-started-with-log4net/

Upvotes: 0

IAmTimCorey
IAmTimCorey

Reputation: 16755

Based on the additional information you provided, there are a couple things I can see. First, you are logging all messages (including DEBUG messages). You just aren't showing log4net debugging information. As for why you are writing to the console window, I would suggest looking at the wrapper for your logger messages. Maybe you are writing to the console there.

Also, walk through the execution of your program in debug mode. See what line is writing to your console.

Upvotes: 1

Alex Aza
Alex Aza

Reputation: 78517

Well, seems like you don't want to give up any more details...

I can think of the folloiwng possible reasons:

  • You have console appender listed either under any <logger> or <root> section
  • You register console appender programmatically
  • You have a logger wrapper, that always runs Console.Write line before logging

Upvotes: 2

Related Questions