Skip to content

Commit

Permalink
NLogLoggingConfiguration - Add support for config variables with Json…
Browse files Browse the repository at this point in the history
…Layout (#465)
  • Loading branch information
snakefoot committed Aug 26, 2021
1 parent d7384e7 commit 5aa3c2d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,6 @@ public LoggingConfigurationElement(IConfigurationSection configurationSection, s

private IEnumerable<KeyValuePair<string, string>> GetValues()
{
var children = _configurationSection.GetChildren();
foreach (var child in children)
{
if (!child.GetChildren().Any())
{
yield return new KeyValuePair<string, string>(GetConfigKey(child), child.Value);
}
}

if (_nameOverride != null)
{
if (ReferenceEquals(_nameOverride, DefaultTargetParameters))
Expand All @@ -182,7 +173,20 @@ public LoggingConfigurationElement(IConfigurationSection configurationSection, s

if (ReferenceEquals(_nameOverride, VariableKey))
{
yield return new KeyValuePair<string, string>("value", _configurationSection.Value);
var value = _configurationSection.Value;
if (value != null)
yield return new KeyValuePair<string, string>("value", value);
else
yield break; // Signal to NLog Config Parser to check GetChildren() for variable layout
}
}

var children = _configurationSection.GetChildren();
foreach (var child in children)
{
if (!child.GetChildren().Any())
{
yield return new KeyValuePair<string, string>(GetConfigKey(child), child.Value);
}
}
}
Expand All @@ -207,10 +211,17 @@ private IEnumerable<ILoggingConfigurationElement> GetChildren()
}
}

var children = _configurationSection.GetChildren();
foreach (var loggingConfigurationElement in GetChildren(children, variables, isTargetsSection))
if (ReferenceEquals(_nameOverride, VariableKey) && _configurationSection.Value == null)
{
yield return new LoggingConfigurationElement(_configurationSection);
}
else
{
yield return loggingConfigurationElement;
var children = _configurationSection.GetChildren();
foreach (var loggingConfigurationElement in GetChildren(children, variables, isTargetsSection))
{
yield return loggingConfigurationElement;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,31 @@ public void LoadVariablesConfig()
Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent()));
}

[Fact]
public void LoadVariableJsonLayoutConfig()
{
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();
memoryConfig["NLog:Targets:file:type"] = "File";
memoryConfig["NLog:Targets:file:fileName"] = "hello.txt";
memoryConfig["NLog:Targets:file:layout"] = "${my_json}";
memoryConfig["NLog:Targets:console:layout"] = "${my_json}";
memoryConfig["NLog:Variables:my_json:type"] = "JsonLayout";
memoryConfig["NLog:Variables:my_json:attributes:0:name"] = "message";
memoryConfig["NLog:Variables:my_json:attributes:0:layout"] = "${message}";
memoryConfig["NLog:Variables:my_json:attributes:1:name"] = "logger";
memoryConfig["NLog:Variables:my_json:attributes:1:layout"] = "${logger}";

var logConfig = CreateNLogLoggingConfigurationWithNLogSection(memoryConfig);

Assert.Single(logConfig.LoggingRules);
Assert.Equal(1, logConfig.Variables.Count);
Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count);
Assert.Equal(2, logConfig.AllTargets.Count);
Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget));
Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget));
Assert.Equal(2, logConfig.AllTargets.Count(t => (t as TargetWithLayout)?.Layout is NLog.Layouts.JsonLayout));
}

[Fact]
public void LoadDefaultWrapperConfig()
{
Expand Down

0 comments on commit 5aa3c2d

Please sign in to comment.