Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NLogLoggingConfiguration - Add support for config variables with JsonLayout #465

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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