Apache log4net� SDK Documentation - Microsoft .NET Framework 4.0

ILog.IsDebugEnabled Property

Checks if this logger is enabled for the Debug level.

[Visual�Basic]
Property�IsDebugEnabled�As�Boolean
[C#]
bool�IsDebugEnabled�{get;}

Property Value

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

Remarks

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

For some ILog interface log, when you write:

[C#]
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:

[C#]
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 Debug. 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:

[C#]
private static readonly bool isDebugEnabled = log.IsDebugEnabled;

Then when you come to log you can write:

[C#]
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

ILog Interface | log4net Namespace | Debug | DebugFormat