Extraneous logging can be expensive. Therefore, I could turn off logging or heighten the logging threshold level to reduce logging during production mode (everybody knows that!)
However, what about the routines that are called to supply parameters to the logger statement?
Consider the logger statement.
logger.info(“number of windows=”+wins.size());
where wins.size() is an excruciatingly expensive operation – say, just for illustration of my (mis)understanding the problem.
If info logging is turned off, would wins.size() still be run.
Because when I do debugging, it seems that wins.size() gets stepped over first before log.info(). If so, what are your strategies to prevent expensive functions inside a logger statement from being executed if the logger is turned off?
In C# 3.0 you could use lambdas to only execute a call to logging function when the logging is turned on. This is example using C# Common.Logging:
where Trace is defined like so:
and FormatMessageHandler is
So what happens is if tracing is turned on the
m=>m("number of windows="+wins.size())lambda gets called. If it’s off, the expensive call to get the window size is never made.