Reinitialize NLog configuration - NLog/NLog GitHub Wiki
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\://localhost}">
Then one can apply the dynamic configuration change like this:
NLog.GlobalDiagnosticsContext.Set("targetUri", "http://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 v5 has introduced this command:
NLog.LogManager.Setup().ReloadConfiguration();
If using NLog v4 then this will also work:
NLog.LogManager.Configuration = NLog.LogManager.Configuration;