if( logger.IsDebugEnabled )

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

if( logger.IsDebugEnabled )

Santhosh Kombiyil

Hi,
Is there any advantage of doing the check before calling the log method. Or if i call the appropriate overloaded function it is good enough to take care of avoiding all the costly string formatting.
 
Is,
 
if( logger.IsDebugEnabled )
{
    logger.Debug( "Doing iteration {0}", 5);
}
 
preferred over,
 
logger.Debug("Doing iteration {0}", 5);
 
Also, please let me know is there a global attribute which i can set in the config file which will turn off logging all together.
 
Thanks,
Santhosh
 
Reply | Threaded
Open this post in threaded view
|

Re: if( logger.IsDebugEnabled )

Jarek Kowalski
Administrator
Santhosh Kombiyil wrote
Hi,
Is there any advantage of doing the check before calling the log method. Or if i call the appropriate overloaded function it is good enough to take care of avoiding all the costly string formatting.

Is,

if( logger.IsDebugEnabled )
{
    logger.Debug( "Doing iteration {0}", 5);
}

preferred over,

logger.Debug("Doing iteration {0}", 5);
You should use IsXXXEnabled when the time needed to construct the parameters can be long:

if (logger.IsDebugEnabled)
{
    logger.Debug("The {0}-th Fibonacci number is {1}", i, Fib(i));
}

If there are no targets assigned to this (logger,level) combination, the Debug() function will do nothing, but the compiler will have to generate the code to call Fib(i), push the results on stack and so on. This can be costly, so you should guard the logging call with the appropriate check.

In general, when you use parameters that are cheap to construct (local variables, member fields, but not method calls) and you use one of the supported overloads, things are very fast.

Santhosh Kombiyil wrote
Also, please let me know is there a global attribute which i can set in the config file which will turn off logging all together.
There's one:

<nlog globalThreshold="Off">
   ...
</nlog>

It is an equivalent of LogManager.GlobalThreshold.
NLog Blog