From 3d48aca3eecf4924520d61154b0dddc2b8fb65b5 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Tue, 2 Apr 2019 22:26:11 +0200 Subject: [PATCH] NLogLoggingConfiguration - Added support for NLog variables --- .../Config/NLogLoggingConfiguration.cs | 86 +++++++++++-------- .../NLog.Extensions.Logging.csproj | 2 +- .../MicrosoftILoggerTargetTests.cs | 2 +- .../NLogLoggingConfigurationTests.cs | 22 ++++- 4 files changed, 73 insertions(+), 39 deletions(-) diff --git a/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs b/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs index cc170164..e7044a2b 100644 --- a/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs +++ b/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs @@ -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 _topElement; + public string Name => _nameOverride ?? _configurationSection.Key; + public IEnumerable> Values => GetValues(); + public IEnumerable 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)) + _topElement = topElement; + if (topElement) { - AutoReload = autoreload; + if (bool.TryParse(configurationSection["autoreload"], out var autoreload)) + { + AutoReload = autoreload; + } } } - public string Name => _nameOverride ?? _configurationSection.Key; - - public IEnumerable> Values + private IEnumerable> 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(child.Key, child.Value); - } - if (_nameOverride != null) - yield return new KeyValuePair("name", _configurationSection.Key); + if (!child.GetChildren().Any()) + yield return new KeyValuePair(child.Key, child.Value); + } + if (_nameOverride != null) + { + yield return new KeyValuePair("name", _configurationSection.Key); + if (ReferenceEquals(_nameOverride, VariableKey)) + yield return new KeyValuePair("value", _configurationSection.Value); } } - public IEnumerable Children + private IEnumerable GetChildren() { - get + var variables = _topElement ? _configurationSection.GetSection(VariablesKey) : null; + if (variables != null) { - var children = _configurationSection.GetChildren(); - foreach (var child in children) + 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 (variables != null && 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); } } } diff --git a/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj b/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj index 02f54712..5533e130 100644 --- a/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj +++ b/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj @@ -56,7 +56,7 @@ See changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHANG $(DefineConstants);NETSTANDARD - + diff --git a/test/NLog.Extensions.Logging.Tests/MicrosoftILoggerTargetTests.cs b/test/NLog.Extensions.Logging.Tests/MicrosoftILoggerTargetTests.cs index 3ede5c84..5c6a608e 100644 --- a/test/NLog.Extensions.Logging.Tests/MicrosoftILoggerTargetTests.cs +++ b/test/NLog.Extensions.Logging.Tests/MicrosoftILoggerTargetTests.cs @@ -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] diff --git a/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs b/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs index 0ee27934..b39ddfb6 100644 --- a/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs +++ b/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs @@ -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); @@ -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] @@ -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 memoryConfig)