Skip to content

Commit

Permalink
Fixed NLogLoggingConfiguration to handle wrapped targets without name (
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Dec 15, 2022
1 parent adc6f2d commit 99c1634
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,26 @@ public LoggingConfigurationElement(IConfigurationSection configurationSection, s
{
yield return new KeyValuePair<string, string>("type", GetConfigKey(_configurationSection));
}
else if (!_topElement)
else if (ReferenceEquals(_nameOverride, VariableKey))
{
var configValue = _configurationSection.Value;
yield return new KeyValuePair<string, string>("name", GetConfigKey(_configurationSection));
if (configValue is null)
yield break; // Signal to NLog Config Parser to check GetChildren() for variable layout
else
yield return new KeyValuePair<string, string>("value", configValue);
}

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

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

[Fact]
public void LoadWrapperConfigExplicitName()
{
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();
memoryConfig["NLog:Targets:file:type"] = "AsyncWrapper";
memoryConfig["NLog:Targets:file:target:type"] = "Memory";
memoryConfig["NLog:Targets:file:target:name"] = "wrappedMem";

var logConfig = CreateNLogLoggingConfigurationWithNLogSection(memoryConfig);

Assert.Single(logConfig.LoggingRules);
Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count);
Assert.Equal(3, logConfig.AllTargets.Count);
Assert.Single(logConfig.AllTargets.Where(t => t is AsyncTargetWrapper));
Assert.Single(logConfig.AllTargets.Where(t => t is MemoryTarget));
Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget));
Assert.NotNull(logConfig.FindTargetByName("wrappedMem") as MemoryTarget);
}

[Fact]
public void LoadWrapperConfigWithoutName()
{
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();
memoryConfig["NLog:Targets:file:type"] = "AsyncWrapper";
memoryConfig["NLog:Targets:file:target:type"] = "Memory";

var logConfig = CreateNLogLoggingConfigurationWithNLogSection(memoryConfig);

Assert.Single(logConfig.LoggingRules);
Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count);
Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget));
Assert.Single(logConfig.AllTargets.Where(t => t is AsyncTargetWrapper));
Assert.True(logConfig.FindTargetByName<AsyncTargetWrapper>("file")?.WrappedTarget is MemoryTarget);
}

[Fact]
public void LoadVariablesConfig()
{
Expand Down

0 comments on commit 99c1634

Please sign in to comment.