IsDebugEnabled vs. Debugging (Action <FormatMessageHandler>)
In the general V2.0 protocol, there are two ways to avoid the cost of evaluating messages when LogLevel is higher than the log entry:
if (Log.IsDebugEnabled)
Log.Debug("Debug message");
or
Log.Debug(a => a("Debug message"));
Which practice is better? What are the pros and cons?
+3
1 answer
According to the documentation:
Using lambdas, the ILog interface offers a new and secure way to record statement logs
log.Debug( m=>m("value= {0}", obj.Value) );
This ensures that the whole expression is evaluated only when LogLevel.Debug is turned on and therefore eliminates the need to write
if (log.IsDebugEnabled)
{
log.Debug("value={0}", obj.Value);
}
to avoid this overhead.
So, the second option in your quesetion is considered best practice.
+6