Search code examples
c#log4net

log4net performance: should i check log level before trying to log?


Would you expect better performance from A) or B) below if log level in the log4net config is set to Info level? Will _log.Debug on its own execute more code and take longer?

A)

if(_log.IsDebugEnabled)
  _log.Debug("some message");

B)

_log.Debug("some message");

Solution

  • In this case, I'd use B.

    However, if constructing the log message (the argument to log.Debug) may take a while - involving significant string concatenation, for example - then I would go for A. It will end up performing the same test twice in the "yes, log it" case, but it won't need to construct the log message in the "no, don't log it" case.