Does static usage of ILog prevent LogLevel adjustment while process is running?

nservicebus

(Philipp Dolder) #1

I’ve found a discussion in a PR that raised a question I would like to understand.
It’s in this PR https://github.com/Particular/NServiceBus/pull/5230
Link to comment / code https://github.com/Particular/NServiceBus/pull/5230#discussion_r209986647

My question:
Doesn’t the static usage of the ILog eliminate the possibility to change the LogLevel while the process is running? e.g. when using log4net.Config.XmlConfigurator.ConfigureAndWatch() I could easily change the log4net.config file to log on a DEBUG level temporarily. But, I suspect the static field ILog would prevent this from happening without unloading the AppDomain (e.g. restarting the process).

Thank you for your clarification

Philipp


(Ramon Smits) #2

No, having a static logger will still result in the ability to change the log level dynamically (atleast for log4net) but what does not work for runtime changing the log level is the following:

static readonly ILog Log = LogManager.GetLogger("MyLogger");
static readonly bool IsDebugEnabled = logger.IsDebugEnabled;

if(IsDebugEnabled)
{
   Log.Debug("My expensive string operation");
}

Such operation will be optimized at first execution by the JIT engine.

Usually this is done because of performance reasons. Always having the Debug logger invoked can results in less performance because often large chunks of string data are concatenated or even objects are serialized and such operations are not cheap.

In such cases you would need to alter the configuration file and just restart your application.

– Ramon


(Philipp Dolder) #3

As I understand you are using this approach inside of NServiceBus in quite some places. so changing the NServiceBus log level to something lower than Info will only work after a restart. But I think, if we’re having troubles on this level it’s ok to quickly restart a service after changing the log level.

I would think that putting the expensive string operation inside an action and only materializing it when it should, the static IsDebugEnabled would not be required to have (almost) the same performance? I’m just asking out of interest and deeper understanding. I’m sure you considered that and decided to go the track with JIT optimazation for a reason.