Skip to content

Commit 4f33779

Browse files
Merge pull request #282 from snakefoot/master
NLogLoggingConfiguration - Added support for NLog variables
2 parents fd54f12 + 3d48aca commit 4f33779

File tree

4 files changed

+73
-39
lines changed

4 files changed

+73
-39
lines changed

src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,64 +46,80 @@ private void LoadConfigurationSection(IConfigurationSection nlogConfig, bool? au
4646

4747
private class LoggingConfigurationElement : ILoggingConfigurationElement
4848
{
49-
readonly IConfigurationSection _configurationSection;
50-
readonly string _nameOverride;
49+
private const string VariablesKey = "Variables";
50+
private const string VariableKey = "Variable";
51+
private const string TargetKey = "target";
52+
private readonly IConfigurationSection _configurationSection;
53+
private readonly string _nameOverride;
54+
private readonly bool _topElement;
5155

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

5461
public LoggingConfigurationElement(IConfigurationSection configurationSection, bool topElement, string nameOverride = null)
5562
{
5663
_configurationSection = configurationSection;
5764
_nameOverride = nameOverride;
58-
if (topElement && bool.TryParse(configurationSection["autoreload"], out var autoreload))
65+
_topElement = topElement;
66+
if (topElement)
5967
{
60-
AutoReload = autoreload;
68+
if (bool.TryParse(configurationSection["autoreload"], out var autoreload))
69+
{
70+
AutoReload = autoreload;
71+
}
6172
}
6273
}
6374

64-
public string Name => _nameOverride ?? _configurationSection.Key;
65-
66-
public IEnumerable<KeyValuePair<string, string>> Values
75+
private IEnumerable<KeyValuePair<string, string>> GetValues()
6776
{
68-
get
77+
var children = _configurationSection.GetChildren();
78+
foreach (var child in children)
6979
{
70-
var children = _configurationSection.GetChildren();
71-
foreach (var child in children)
72-
{
73-
if (!child.GetChildren().Any())
74-
yield return new KeyValuePair<string, string>(child.Key, child.Value);
75-
}
76-
if (_nameOverride != null)
77-
yield return new KeyValuePair<string, string>("name", _configurationSection.Key);
80+
if (!child.GetChildren().Any())
81+
yield return new KeyValuePair<string, string>(child.Key, child.Value);
82+
}
83+
if (_nameOverride != null)
84+
{
85+
yield return new KeyValuePair<string, string>("name", _configurationSection.Key);
86+
if (ReferenceEquals(_nameOverride, VariableKey))
87+
yield return new KeyValuePair<string, string>("value", _configurationSection.Value);
7888
}
7989
}
8090

81-
public IEnumerable<ILoggingConfigurationElement> Children
91+
private IEnumerable<ILoggingConfigurationElement> GetChildren()
8292
{
83-
get
93+
var variables = _topElement ? _configurationSection.GetSection(VariablesKey) : null;
94+
if (variables != null)
8495
{
85-
var children = _configurationSection.GetChildren();
86-
foreach (var child in children)
96+
foreach (var variable in variables.GetChildren())
97+
yield return new LoggingConfigurationElement(variable, false, VariableKey);
98+
}
99+
100+
var children = _configurationSection.GetChildren();
101+
foreach (var child in children)
102+
{
103+
var firstChildValue = child?.GetChildren()?.FirstOrDefault();
104+
if (firstChildValue == null)
105+
continue; // Simple value without children
106+
107+
if (_nameOverride == TargetKey && child.Key.EqualsOrdinalIgnoreCase(TargetKey) && child.GetChildren().Count() == 1)
87108
{
88-
var firstChildValue = child?.GetChildren()?.FirstOrDefault();
89-
if (firstChildValue == null)
90-
{
109+
// Target-config inside Wrapper-Target
110+
yield return new LoggingConfigurationElement(firstChildValue, false, TargetKey);
111+
}
112+
else
113+
{
114+
if (variables != null && string.Equals(child.Key, VariablesKey, StringComparison.OrdinalIgnoreCase))
91115
continue;
92-
}
93116

94-
if (_nameOverride == "target" && child.Key.EqualsOrdinalIgnoreCase("target") && child.GetChildren().Count() == 1)
95-
{
96-
yield return new LoggingConfigurationElement(firstChildValue, false, "target");
97-
}
98-
else
117+
string nameOverride = null;
118+
if (_configurationSection.Key.EqualsOrdinalIgnoreCase("targets"))
99119
{
100-
string nameOverride = null;
101-
if (_configurationSection.Key.EqualsOrdinalIgnoreCase("targets"))
102-
{
103-
nameOverride = "target";
104-
}
105-
yield return new LoggingConfigurationElement(child, false, nameOverride);
120+
nameOverride = TargetKey;
106121
}
122+
yield return new LoggingConfigurationElement(child, false, nameOverride);
107123
}
108124
}
109125
}

src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ See changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHANG
5656
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
5757
</PropertyGroup>
5858
<ItemGroup>
59-
<PackageReference Include="NLog" Version="[4.6.1,5.0.0-beta01)" />
59+
<PackageReference Include="NLog" Version="[4.6.2,5.0.0-beta01)" />
6060
</ItemGroup>
6161
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
6262
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="1.0.0" />

test/NLog.Extensions.Logging.Tests/MicrosoftILoggerTargetTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void FilterILoggerMessageTest()
3333
logFactory.Configuration = logConfig;
3434
var logger = logFactory.GetCurrentClassLogger();
3535
logger.Debug("Hello World");
36-
Assert.Equal(null, ilogger.LastLogMessage);
36+
Assert.Null(ilogger.LastLogMessage);
3737
}
3838

3939
[Fact]

test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ public class NLogLoggingConfigurationTests
1414
public void LoadSimpleConfig()
1515
{
1616
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();
17-
1817
memoryConfig["NLog:Targets:file:type"] = "File";
1918
memoryConfig["NLog:Targets:file:fileName"] = "hello.txt";
20-
memoryConfig["NLog:Targets:console:type"] = "Console";
2119

2220
var logConfig = CreateNLogLoggingConfigurationWithNLogSection(memoryConfig);
2321

@@ -26,6 +24,7 @@ public void LoadSimpleConfig()
2624
Assert.Equal(2, logConfig.AllTargets.Count);
2725
Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget));
2826
Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget));
27+
Assert.Equal("hello.txt", (logConfig.FindTargetByName("File") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent()));
2928
}
3029

3130
[Fact]
@@ -44,6 +43,25 @@ public void LoadWrapperConfig()
4443
Assert.Single(logConfig.AllTargets.Where(t => t is AsyncTargetWrapper));
4544
Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget));
4645
Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget));
46+
Assert.Equal("hello.txt", (logConfig.FindTargetByName("wrappedFile") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent()));
47+
}
48+
49+
[Fact]
50+
public void LoadVariablesConfig()
51+
{
52+
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();
53+
memoryConfig["NLog:Targets:file:type"] = "File";
54+
memoryConfig["NLog:Targets:file:fileName"] = "${var_filename}";
55+
memoryConfig["NLog:Variables:var_filename"] = "hello.txt";
56+
57+
var logConfig = CreateNLogLoggingConfigurationWithNLogSection(memoryConfig);
58+
59+
Assert.Single(logConfig.LoggingRules);
60+
Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count);
61+
Assert.Equal(2, logConfig.AllTargets.Count);
62+
Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget));
63+
Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget));
64+
Assert.Equal("hello.txt", (logConfig.FindTargetByName("File") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent()));
4765
}
4866

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

0 commit comments

Comments
 (0)