4
votes

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>
3
I used the above config in a brand new console project and it does the same thing. Only one appender, and it still sends to the console window. What gives? :/Tom Whitaker
Think the missing part here was code to create an instance of the logger, he might of had a similar problem to me stackoverflow.com/a/18122298/427684Coops

3 Answers

1
votes

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
1
votes

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.

0
votes

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/