diff --git a/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs b/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs index 567b5ada..7b43eb0a 100644 --- a/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs +++ b/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs @@ -97,8 +97,14 @@ private void ReloadConfigurationSection(IConfigurationSection nlogConfig) InternalLogger.Info("Reloading NLogLoggingConfiguration..."); var newConfig = new NLogLoggingConfiguration(nlogConfig, LogFactory); - if (LogFactory.Configuration != null) + var oldConfig = LogFactory.Configuration; + if (oldConfig != null) { + if (LogFactory.KeepVariablesOnReload) + { + foreach (var variable in oldConfig.Variables) + newConfig.Variables[variable.Key] = variable.Value; + } LogFactory.Configuration = newConfig; } } diff --git a/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs b/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs index f5f8d879..60f9a58a 100644 --- a/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs +++ b/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs @@ -108,14 +108,14 @@ public void LoadDefaultTargetParametersJsonLayoutConfig() { var memoryConfig = CreateMemoryConfigConsoleTargetAndRule(); memoryConfig["NLog:Targets:file:type"] = "File"; - memoryConfig["NLog:Default-target-parameters:file:filename"] = "hello.txt"; - memoryConfig["NLog:Default-target-parameters:file:layout:type"] = "JsonLayout"; - memoryConfig["NLog:Default-target-parameters:file:layout:Attributes:0:name"] = "timestamp"; - memoryConfig["NLog:Default-target-parameters:file:layout:Attributes:0:layout"] = "${date:format=o}"; - memoryConfig["NLog:Default-target-parameters:file:layout:Attributes:1:name"] = "level"; - memoryConfig["NLog:Default-target-parameters:file:layout:Attributes:1:layout"] = "${level}"; - memoryConfig["NLog:Default-target-parameters:file:layout:Attributes:2:name"] = "message"; - memoryConfig["NLog:Default-target-parameters:file:layout:Attributes:2:layout"] = "${message}"; + memoryConfig["NLog:default-target-parameters:file:filename"] = "hello.txt"; + memoryConfig["NLog:default-target-parameters:file:layout:type"] = "JsonLayout"; + memoryConfig["NLog:default-target-parameters:file:layout:Attributes:0:name"] = "timestamp"; + memoryConfig["NLog:default-target-parameters:file:layout:Attributes:0:layout"] = "${date:format=o}"; + memoryConfig["NLog:default-target-parameters:file:layout:Attributes:1:name"] = "level"; + memoryConfig["NLog:default-target-parameters:file:layout:Attributes:1:layout"] = "${level}"; + memoryConfig["NLog:default-target-parameters:file:layout:Attributes:2:name"] = "message"; + memoryConfig["NLog:default-target-parameters:file:layout:Attributes:2:layout"] = "${message}"; var logConfig = CreateNLogLoggingConfigurationWithNLogSection(memoryConfig); @@ -150,6 +150,26 @@ private void ReloadLogFactoryConfiguration() configuration.Reload(); // Nothing should happen } + [Fact] + private void ReloadLogFactoryConfigurationKeepVariables() + { + var memoryConfig = CreateMemoryConfigConsoleTargetAndRule(); + memoryConfig["NLog:Targets:file:type"] = "File"; + memoryConfig["NLog:Targets:file:fileName"] = "${var:var_filename}"; + memoryConfig["NLog:autoreload"] = "true"; + memoryConfig["NLog:KeepVariablesOnReload"] = "true"; + memoryConfig["NLog:variables:var_filename"] = "hello.txt"; + var configuration = new ConfigurationBuilder().AddInMemoryCollection(memoryConfig).Build(); + var logFactory = new LogFactory(); + var logConfig = new NLogLoggingConfiguration(configuration.GetSection("NLog"), logFactory); + logFactory.Configuration = logConfig; + Assert.Equal("hello.txt", (logFactory.Configuration.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); + logFactory.Configuration.Variables["var_filename"] = "updated.txt"; + Assert.Equal("updated.txt", (logFactory.Configuration.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); + configuration.Reload(); // Automatic Reload + Assert.Equal("updated.txt", (logFactory.Configuration.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); + } + private static NLogLoggingConfiguration CreateNLogLoggingConfigurationWithNLogSection(IDictionary memoryConfig) { var configuration = new ConfigurationBuilder().AddInMemoryCollection(memoryConfig).Build();