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

Reading JSON config - Trim keys as Json allows spaces in keys #419

Merged
merged 1 commit into from
May 11, 2020
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
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