Skip to content

Commit

Permalink
NLogLoggingConfiguration - Support KeepVariablesOnReload (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored and 304NotModified committed Apr 28, 2019
1 parent cef73eb commit 1e5180d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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<string, string> memoryConfig)
{
var configuration = new ConfigurationBuilder().AddInMemoryCollection(memoryConfig).Build();
Expand Down

0 comments on commit 1e5180d

Please sign in to comment.