Skip to content

Commit

Permalink
NLogLoggingConfiguration - Trim config-keys as Json allows spaces in …
Browse files Browse the repository at this point in the history
…keys (#419)
  • Loading branch information
snakefoot committed May 11, 2020
1 parent 4bd3bcb commit 034f279
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
32 changes: 23 additions & 9 deletions src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public LoggingConfigurationElement(IConfigurationSection configurationSection, b

public bool AutoReload { get; }

public string Name => _nameOverride ?? _configurationSection.Key;
public string Name => _nameOverride ?? GetConfigKey(_configurationSection);
public IEnumerable<KeyValuePair<string, string>> Values => GetValues();
public IEnumerable<ILoggingConfigurationElement> Children => GetChildren();

Expand All @@ -162,19 +162,19 @@ public LoggingConfigurationElement(IConfigurationSection configurationSection, b
{
if (!child.GetChildren().Any())
{
yield return new KeyValuePair<string, string>(child.Key, child.Value);
yield return new KeyValuePair<string, string>(GetConfigKey(child), child.Value);
}
}

if (_nameOverride != null)
{
if (ReferenceEquals(_nameOverride, DefaultTargetParameters))
{
yield return new KeyValuePair<string, string>("type", _configurationSection.Key);
yield return new KeyValuePair<string, string>("type", GetConfigKey(_configurationSection));
}
else
{
yield return new KeyValuePair<string, string>("name", _configurationSection.Key);
yield return new KeyValuePair<string, string>("name", GetConfigKey(_configurationSection));
}

if (ReferenceEquals(_nameOverride, VariableKey))
Expand Down Expand Up @@ -241,17 +241,31 @@ private IEnumerable<ILoggingConfigurationElement> GetChildren(IEnumerable<IConfi

private static LoggingConfigurationElement CreateLoggingConfigurationElement(bool isTargetsSection, IConfigurationSection child, IConfigurationSection defaultTargetParameters, IConfigurationSection defaultTargetWrapper)
{
var isTargetKey = child.Key.EqualsOrdinalIgnoreCase(TargetsKey);
IConfigurationSection defaultTargetParametersSection = null;
IConfigurationSection defaultTargetWrapperSection = null;

if (defaultTargetParameters != null || defaultTargetWrapper != null)
{
var isTargetsKey = GetConfigKey(child).EqualsOrdinalIgnoreCase(TargetsKey);
defaultTargetParametersSection = defaultTargetParameters != null && isTargetsKey ? defaultTargetParameters : null;
defaultTargetWrapperSection = defaultTargetWrapper != null && isTargetsKey ? defaultTargetWrapper : null;
}

return new LoggingConfigurationElement(child, false, isTargetsSection ? TargetKey : null)
{
DefaultTargetParametersSection = defaultTargetParameters != null && isTargetKey ? defaultTargetParameters : null,
DefaultTargetWrapperSection = defaultTargetWrapper != null && isTargetKey ? defaultTargetWrapper : null,
DefaultTargetParametersSection = defaultTargetParametersSection,
DefaultTargetWrapperSection = defaultTargetWrapperSection,
};
}

private static string GetConfigKey(IConfigurationSection child)
{
return child.Key?.Trim() ?? string.Empty;
}

private bool IsTargetsSection()
{
return !_topElement && _nameOverride == null && _configurationSection.Key.EqualsOrdinalIgnoreCase(TargetsKey);
return !_topElement && _nameOverride == null && GetConfigKey(_configurationSection).EqualsOrdinalIgnoreCase(TargetsKey);
}

/// <summary>
Expand All @@ -261,7 +275,7 @@ private bool IsTargetsSection()
/// <returns></returns>
private bool IsTargetWithinWrapper(IConfigurationSection child)
{
return _nameOverride == TargetKey && child.Key.EqualsOrdinalIgnoreCase(TargetKey) && child.GetChildren().Count() == 1;
return _nameOverride == TargetKey && GetConfigKey(child).EqualsOrdinalIgnoreCase(TargetKey) && child.GetChildren().Count() == 1;
}

private IEnumerable<LoggingConfigurationElement> GetTargetsDefaultConfigElements()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,24 @@ public void LoadSimpleConfig()
Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget));
Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent()));
}


[Fact]
public void LoadSimpleConfigAndTrimSpace()
{
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();
memoryConfig["NLog:Targets:file:type"] = "File";
memoryConfig["NLog:Targets:file:fileName "] = "hello.txt";

var logConfig = CreateNLogLoggingConfigurationWithNLogSection(memoryConfig);

Assert.Single(logConfig.LoggingRules);
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("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent()));
}

[Fact]
public void LoadWrapperConfig()
{
Expand Down

0 comments on commit 034f279

Please sign in to comment.