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

Fixed NLogLoggingConfiguration to handle wrapped targets without name #650

Merged
merged 1 commit into from
Dec 15, 2022
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
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 @@ private IEnumerable<KeyValuePair<string, string>> GetValues()
{
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