-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Reinitialize NLog configuration
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.
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.
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
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.
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
- Articles about NLog
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json