Skip to content

Commit

Permalink
Merge pull request #282 from snakefoot/master
Browse files Browse the repository at this point in the history
NLogLoggingConfiguration - Added support for NLog variables
  • Loading branch information
304NotModified authored Apr 3, 2019
2 parents fd54f12 + 3d48aca commit 4f33779
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 39 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 _topElement;

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))
_topElement = topElement;
if (topElement)
{
AutoReload = autoreload;
if (bool.TryParse(configurationSection["autoreload"], out var autoreload))
{
AutoReload = autoreload;
}
}
}

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
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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ See changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHANG
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="[4.6.1,5.0.0-beta01)" />
<PackageReference Include="NLog" Version="[4.6.2,5.0.0-beta01)" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="1.0.0" />
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 4f33779

Please sign in to comment.