Skip to content

Reinitialize NLog configuration

Rolf Kristensen edited this page May 9, 2019 · 3 revisions

The combination of dynamic logging configuration and NLog-targets that only applies configuration during initialization can give a catch22. You want to have logging up and running early, but this will fail if NLog-targets initialization depends on configuration settings being available. This can lead to NLog-targets failing to initialize so NLog suddenly is disabled, or NLog-targets never using the configured destination.

GDC and whenEmpty

The recommended solution to this problem is to make use of GDC and whenEmpty:

<target xsi:type="ElasticSearch" uri="${gdc:item=targetUri:whenEmpty=http\:https://localhost}">

Then one can apply the dynamic configuration change like this:

NLog.GlobalDiagnosticsContext.Set("targetUri", "https://127.0.0.1:9200");
NLog.LogManager.Configuration = NLog.LogManager.Configuration?.Reload();

Please note that Configuration.Reload() will cause all NLog-variables to be reset. This is why GDC is being used to store the dynamic configuration.

Re-initialize single target

The standard NLog targets supports reinitialization, but it is not always supported by 3rdParty NLog targets. This can happen if closing NLog-target performs dispose of members, that are only initialized in the constructor of the NLog-target.

var target = NLog.LogManager.Configuration?.FindTargetByName<BlobStorageTarget>("blob");
target?.Dispose();   // Closes the target so it is uninitialized
NLog.LogManager.ReconfigExistingLoggers(); // Ensures all targets are initialized

Knowing the name of the NLog-target can sometimes be a challenge, especially if the NLog-target is wrapped using AsyncWrapper or BufferingWrapper. One can also do this:

NLog.LogManager.Configuration?.AllTargets.OfType<BlobStorageTarget>().ToList().ForEach(t => t.Dispose());
NLog.LogManager.ReconfigExistingLoggers(); // Ensures all targets are initialized

Re-initialize all targets

This is the big hammer, and might not be supported by all NLog-targets. This can happen if closing NLog-target performs dispose of members, that are only initialized in the constructor of the NLog-target.

NLog.LogManager.Configuration = NLog.LogManager.Configuration;
Clone this wiki locally