ILogIsDebugEnabled Property Apache log4net™ SDK Documentation
Checks if this logger is enabled for the Debug level.

Namespace: log4net
Assembly: log4net (in log4net.dll) Version: 4.0

bool IsDebugEnabled { get; }

Property Value

Type: Boolean
true if this logger is enabled for Debug events, false otherwise.

This function is intended to lessen the computational cost of disabled log debug statements.

For some ILog interface log, when you write:

log.Debug("This is entry number: " + i );

You incur the cost constructing the message, string construction and concatenation in this case, regardless of whether the message is logged or not.

If you are worried about speed (who isn't), then you should write:

if (log.IsDebugEnabled)
    log.Debug("This is entry number: " + i );

This way you will not incur the cost of parameter construction if debugging is disabled for log. On the other hand, if the log is debug enabled, you will incur the cost of evaluating whether the logger is debug enabled twice. Once in IsDebugEnabled and once in the [M:Debug(object)]. This is an insignificant overhead since evaluating a logger takes about 1% of the time it takes to actually log. This is the preferred style of logging.

Alternatively if your logger is available statically then the is debug enabled state can be stored in a static variable like this:

private static readonly bool isDebugEnabled = log.IsDebugEnabled;

Then when you come to log you can write:

if (isDebugEnabled)
    log.Debug("This is entry number: " + i );

This way the debug enabled state is only queried once when the class is loaded. Using a private static readonly variable is the most efficient because it is a run time constant and can be heavily optimized by the JIT compiler.

Of course if you use a static readonly variable to hold the enabled state of the logger then you cannot change the enabled state at runtime to vary the logging that is produced. You have to decide if you need absolute speed or runtime flexibility.

See Also


[M:DebugFormat(IFormatProvider, string, object[])]