Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 680
HuBeZa Avatar asked Nov 19 '25 22:11

HuBeZa


1 Answers

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.

like image 154
Restuta Avatar answered Nov 22 '25 03:11

Restuta