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");
log4net offers the following log levels, in increasing order of priority: ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF. The ALL level logs everything and the OFF level logs nothing.
Is log4net thread-safe? Yes, log4net is thread-safe.
Logging levels are used to filter logging output, tailoring the amount of data output to the situation in hand. Each logging level is associated with the type of data logged. DEBUG, INFO, and TRACE events are typically non-error conditions that report on the behavior of an application.
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.
I would choose option B unless the log message itself takes a long time to construct. The performance gain for the usual case is negligible or doesn't even exist. Internally, log4net does the same check so you won't change anything by doing it yourself.
But as I said, option A may be a good idea in a situation like this:
if (_log.IsDebugEnabled())
{
var message = createComplicatedLogMessageThatTakesALotOfTime();
_log.Debug(message);
}
For all other cases, it just adds three more lines for every message you log which just isn't worth the effort.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With