Skip to content

Commit

Permalink
NLogLoggingConfiguration - Added support for NLog variables
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Apr 2, 2019
1 parent fd54f12 commit 1ddabe6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 38 deletions.
86 changes: 51 additions & 35 deletions src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,64 +46,80 @@ private void LoadConfigurationSection(IConfigurationSection nlogConfig, bool? au

private class LoggingConfigurationElement : ILoggingConfigurationElement
{
readonly IConfigurationSection _configurationSection;
readonly string _nameOverride;
private const string VariablesKey = "Variables";
private const string VariableKey = "Variable";
private const string TargetKey = "target";
private readonly IConfigurationSection _configurationSection;
private readonly string _nameOverride;
private readonly bool _hasVariables;

public string Name => _nameOverride ?? _configurationSection.Key;
public IEnumerable<KeyValuePair<string, string>> Values => GetValues();
public IEnumerable<ILoggingConfigurationElement> Children => GetChildren();
public bool AutoReload { get; }

public LoggingConfigurationElement(IConfigurationSection configurationSection, bool topElement, string nameOverride = null)
{
_configurationSection = configurationSection;
_nameOverride = nameOverride;
if (topElement && bool.TryParse(configurationSection["autoreload"], out var autoreload))
if (topElement)
{
AutoReload = autoreload;
if (bool.TryParse(configurationSection["autoreload"], out var autoreload))
{
AutoReload = autoreload;
}
_hasVariables = configurationSection.GetSection(VariablesKey) != null;
}
}

public string Name => _nameOverride ?? _configurationSection.Key;

public IEnumerable<KeyValuePair<string, string>> Values
private IEnumerable<KeyValuePair<string, string>> GetValues()
{
get
var children = _configurationSection.GetChildren();
foreach (var child in children)
{
var children = _configurationSection.GetChildren();
foreach (var child in children)
{
if (!child.GetChildren().Any())
yield return new KeyValuePair<string, string>(child.Key, child.Value);
}
if (_nameOverride != null)
yield return new KeyValuePair<string, string>("name", _configurationSection.Key);
if (!child.GetChildren().Any())
yield return new KeyValuePair<string, string>(child.Key, child.Value);
}
if (_nameOverride != null)
{
yield return new KeyValuePair<string, string>("name", _configurationSection.Key);
if (ReferenceEquals(_nameOverride, VariableKey))
yield return new KeyValuePair<string, string>("value", _configurationSection.Value);
}
}

public IEnumerable<ILoggingConfigurationElement> Children
private IEnumerable<ILoggingConfigurationElement> GetChildren()
{
get
if (_hasVariables)
{
var children = _configurationSection.GetChildren();
foreach (var child in children)
var variables = _configurationSection.GetSection(VariablesKey);
foreach (var variable in variables.GetChildren())
yield return new LoggingConfigurationElement(variable, false, VariableKey);
}

var children = _configurationSection.GetChildren();
foreach (var child in children)
{
var firstChildValue = child?.GetChildren()?.FirstOrDefault();
if (firstChildValue == null)
continue; // Simple value without children

if (_nameOverride == TargetKey && child.Key.EqualsOrdinalIgnoreCase(TargetKey) && child.GetChildren().Count() == 1)
{
var firstChildValue = child?.GetChildren()?.FirstOrDefault();
if (firstChildValue == null)
{
// Target-config inside Wrapper-Target
yield return new LoggingConfigurationElement(firstChildValue, false, TargetKey);
}
else
{
if (_hasVariables && string.Equals(child.Key, VariablesKey, StringComparison.OrdinalIgnoreCase))
continue;
}

if (_nameOverride == "target" && child.Key.EqualsOrdinalIgnoreCase("target") && child.GetChildren().Count() == 1)
{
yield return new LoggingConfigurationElement(firstChildValue, false, "target");
}
else
string nameOverride = null;
if (_configurationSection.Key.EqualsOrdinalIgnoreCase("targets"))
{
string nameOverride = null;
if (_configurationSection.Key.EqualsOrdinalIgnoreCase("targets"))
{
nameOverride = "target";
}
yield return new LoggingConfigurationElement(child, false, nameOverride);
nameOverride = TargetKey;
}
yield return new LoggingConfigurationElement(child, false, nameOverride);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void FilterILoggerMessageTest()
logFactory.Configuration = logConfig;
var logger = logFactory.GetCurrentClassLogger();
logger.Debug("Hello World");
Assert.Equal(null, ilogger.LastLogMessage);
Assert.Null(ilogger.LastLogMessage);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ public class NLogLoggingConfigurationTests
public void LoadSimpleConfig()
{
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();

memoryConfig["NLog:Targets:file:type"] = "File";
memoryConfig["NLog:Targets:file:fileName"] = "hello.txt";
memoryConfig["NLog:Targets:console:type"] = "Console";

var logConfig = CreateNLogLoggingConfigurationWithNLogSection(memoryConfig);

Expand All @@ -26,6 +24,7 @@ public void LoadSimpleConfig()
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]
Expand All @@ -44,6 +43,25 @@ public void LoadWrapperConfig()
Assert.Single(logConfig.AllTargets.Where(t => t is AsyncTargetWrapper));
Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget));
Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget));
Assert.Equal("hello.txt", (logConfig.FindTargetByName("wrappedFile") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent()));
}

[Fact]
public void LoadVariablesConfig()
{
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();
memoryConfig["NLog:Targets:file:type"] = "File";
memoryConfig["NLog:Targets:file:fileName"] = "${var_filename}";
memoryConfig["NLog:Variables:var_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()));
}

private static NLogLoggingConfiguration CreateNLogLoggingConfigurationWithNLogSection(IDictionary<string, string> memoryConfig)
Expand Down

0 comments on commit 1ddabe6

Please sign in to comment.