DayTwo DayTwo - 1 month ago 10x
C# Question

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?


_log.Debug("some message");


_log.Debug("some message");


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.