IsDebugEnabled vs. Debug(Action<FormatMessageHandler>)
In common logging V2.0 there are two methods of avoiding开发者_开发技巧 costs of message evaluation 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 & cons?
According to documentation:
Leveraging lambdas, the ILog interface offers a new & safe way to write log statements
log.Debug( m=>m("value= {0}", obj.Value) );
This ensures, that the whole expression is only evaluated when LogLevel.Debug is enabled and thus saves you from having to write
if (log.IsDebugEnabled)
{
log.Debug("value={0}", obj.Value);
}
to avoid this overhead.
So the second option in your quesetion considered a best practice.
精彩评论