I’m curious as to why I see people write log4net logging code like the following:
if (_logger.IsDebugEnabled)
{
_logger.Debug("Some debug text");
}
I’ve gone through the disassembly for log4net, and calling Debug makes another call to the same code to see if it’s enabled before actually logging, so the IsDebugEnabled call is unnecessary and actually is duplicated code.
Is there a reason people do this? Maybe an old pattern that used to be necessary in older versions but isn’t anymore? Or could there be a legitimate reason for it? Or maybe people just don’t know that they don’t need to do it?
This same behavior is there for the other levels (Info, Error, Warn, Finest, etc.) as well.
The message could be expensive to build. Wrapping it in the
ifstatement ensures it is only created when necessary.Another pattern that addresses this issue is:
I don’t know if log4net supports anything like that, though.