3

I have the following code:

private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
XmlConfigurator.Configure(new FileInfo("log4net.config"));

The problem is the statement below always returns True

if (Log.IsDebugEnabled) { /* do time consuming stuff and log the result */

This is my log4net config file_

  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <filter type="log4net.Filter.LevelRangeFilter">
      <param name="LevelMin" value="ERROR" />
      <param name="LevelMax" value="ERROR" />
    </filter>

    <param name="File" value="Log.txt" />
    <param name="AppendToFile" value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />

    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%5p [%d] - %m%n" />
    </layout>

  </appender>

  <root>
    <appender-ref ref="LogFileAppender" />
    <appender-ref ref="ConsoleAppender" />
  </root>

Why does IsDebugEnabled always return True?

Isaac-Johnson
  • 33
  • 1
  • 3

4 Answers4

6

From the log4net manual:

Note that by default, the root logger is assigned to Level.DEBUG.

Try:

<root>
    <level value="INFO"/>
</root>

Note that IsDebugEnabled is controlled by the logger level. The fact that you have LevelMin/LevelMax filters on all your appenders doesn't help, because of the loose coupling between loggers and appenders.

Joe
  • 122,218
  • 32
  • 205
  • 338
1

define rootCategory for RollingFileAppender

Tausif Baber
  • 500
  • 2
  • 8
  • 23
1

You need to call

XmlConfigurator.Configure(new FileInfo("log4net.config"));

before you call the LogManager.GetLogger method. I would recommend doing this in startup of your application. Because with the code the way it is now, it is not configuring the logger until after you have already instantiated it.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
Paige Cook
  • 22,415
  • 3
  • 57
  • 68
0

That's because the Root Level Value is not been Set.

<root>
  <appender-ref ref="LogFileAppender" />
  <appender-ref ref="ConsoleAppender" />
</root>

By Default, it will take all the Levels. By defining the Root Level Value to INFO,WARN,ERROR,and FATAL will solve your issue.

Liam McInroy
  • 4,339
  • 5
  • 32
  • 53