Skip to content

Reinitialize NLog configuration

Rolf Kristensen edited this page May 29, 2025 · 6 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\://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;

Notice that LogEvents produced by the application can become lost, in the period where closing and initializing all NLog targets.

Clone this wiki locally
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy