NLog is where we are NET domain uses a very wide range of logging components. It uses xml by default to maintain its configuration. Recently, several students asked me how to configure NLog when using AgileConfig. Because AgileConfig does not support the configuration of integrated xml format. In fact, NLog supports from appsettings JSON / iconfiguration reads the configuration, so there must be no problem with our AgileConfig collection. The following describes how NLog integrates with AgileConfig and supports dynamic configuration.
Configuring NLog using AgileConfig
The default configuration of NLog is configured through xml. Now our NET programs are mostly through appsettings JSON. NLog provides from appsettings JSON / IConfiguration read the extension of the configuration. Since IConfiguration reading is supported, it is very simple to follow our AgileConfig.
Modify program cs
Install from nuget:
copyNLog.Extensions.Hosting NLog.Web.AspNetCore
Use the UseAgileConfig extension to turn on AgileConfig support. In the builder Services. Manually set logmanager in addlogging method Value of configuration.
copy//use agileconfig client builder.Host.UseAgileConfig(); //add nlog porvider builder.Services.AddLogging(b => { b.ClearProviders(); IConfiguration config = builder.Configuration; NLog.LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog")); b.AddNLogWeb(); });
Maintain configuration in AgileConfig
After modifying the code, we need to maintain the json configuration file on AgileConfig. The basic use of AgileConfig will not be repeated. See the previous article. AgileConfig data .
- New application Nlog_test Create an application NLog on the AgileConfig console_ test .

- Maintain Nlog configuration Maintain the following json configuration to Nlog_test application.
copy{ "NLog": { "rules": [ { "logger": "*", "minLevel": "Trace", "writeTo": "logfile2" } ], "targets": { "async": "True", "logconsole": { "type": "Console" }, "logfile1": { "fileName": "d:/nlogs/nlog-${shortdate}.log", "type": "File" }, "logfile2": { "fileName": "d:/nlogs/nlog-${shortdate}-file2.log", "type": "File" } }, "throwConfigExceptions": "True" } }
Copy the above JSON file and paste it into the "Edit JSON" text box:

- Publishing configuration Click the Publish button to launch Nlog configuration.

Running items
After running the project, we can see that the log has been written to the specified location, indicating that Nlog has successfully read the configuration from AgileConfig.
Dynamically refresh NLog configuration
In the above code, we implemented NLog by reading the configuration from agileconfig without xml, but our configuration is one-time. When we modify the configuration on the agileconfig console, we will not change the NLog configuration. This obviously does not conform to our agileconfig dynamic configuration temperament. Since NLog will not automatically listen for IConfiguration changes, let's manually reload NLog configuration through the agileconfig configuration change event.
copyvoid loadNlogConfig() { IConfiguration config = builder.Configuration; NLog.LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog")); NLog.LogManager.Configuration.Reload(); } //use agileconfig client builder.Host.UseAgileConfig((ConfigChangedArg e) => { loadNlogConfig(); }); //add nlog porvider builder.Services.AddLogging(b => { b.ClearProviders(); NLog.LogManager.ConfigurationChanged += (_, _) => NLog.LogManager.ReconfigExistingLoggers(); loadNlogConfig(); b.AddNLogWeb(); });
Through the above configuration, when we modify Nlog configuration rules in AgileConfig, as long as we click publish, the applied Nlog configuration will be changed in real time.
AgileConfig
AgileConfig is a lightweight configuration center ✨✨✨ Github address: https://github.com/dotnetcore/AgileConfig Open source is not easy, welcome star ✨✨✨
Demo address: http://agileconfig_server.xbaby.xyz/ Super administrator account: admin Password: 123456