Skip to content

Commit

Permalink
NLogLoggingConfiguration support custom section names (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
anellomancante authored Mar 13, 2021
1 parent b6955c5 commit a047ac1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class NLogLoggingConfiguration : LoggingConfigurationParser
private bool _autoReload;
private Action<object> _reloadConfiguration;
private IDisposable _registerChangeCallback;
private const string RootSectionKey = "NLog";

/// <summary>
/// Initializes a new instance of the <see cref="NLogLoggingConfiguration" /> class.
Expand Down Expand Up @@ -64,7 +65,7 @@ public override LoggingConfiguration Reload()

private bool LoadConfigurationSection(IConfigurationSection nlogConfig)
{
var configElement = new LoggingConfigurationElement(nlogConfig, true);
var configElement = new LoggingConfigurationElement(nlogConfig, true, RootSectionKey);
LoadConfig(configElement, null);
return configElement.AutoReload;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace NLog.Extensions.Logging.Tests
{
public class NLogLoggingConfigurationTests
{
const string DefaultSectionName = "NLog";
const string CustomSectionName = "MyCustomSection";

#if !NETCORE1_0
[Fact]
public void LoadSimpleConfig()
Expand All @@ -28,6 +31,23 @@ public void LoadSimpleConfig()
Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent()));
}

[Fact]
public void LoadSimpleConfigWithCustomKey()
{
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule(CustomSectionName);
memoryConfig[$"{CustomSectionName}:Targets:file:type"] = "File";
memoryConfig[$"{CustomSectionName}:Targets:file:fileName"] = "hello.txt";

var logConfig = CreateNLogLoggingConfigurationWithNLogSection(memoryConfig, CustomSectionName);

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 LoadSimpleConfigAndTrimSpace()
{
Expand Down Expand Up @@ -204,21 +224,21 @@ public void SetupBuilderLoadConfigurationFromSection()
Assert.Equal(2, logFactory.Configuration.LoggingRules[0].Targets.Count);
}

private static NLogLoggingConfiguration CreateNLogLoggingConfigurationWithNLogSection(IDictionary<string, string> memoryConfig)
private static NLogLoggingConfiguration CreateNLogLoggingConfigurationWithNLogSection(IDictionary<string, string> memoryConfig, string sectionName = DefaultSectionName)
{
var configuration = new ConfigurationBuilder().AddInMemoryCollection(memoryConfig).Build();
var logFactory = new LogFactory();
var logConfig = new NLogLoggingConfiguration(configuration.GetSection("NLog"), logFactory);
var logConfig = new NLogLoggingConfiguration(configuration.GetSection(sectionName), logFactory);
return logConfig;
}

private static Dictionary<string, string> CreateMemoryConfigConsoleTargetAndRule()
private static Dictionary<string, string> CreateMemoryConfigConsoleTargetAndRule(string sectionName = DefaultSectionName)
{
var memoryConfig = new Dictionary<string, string>();
memoryConfig["NLog:Rules:0:logger"] = "*";
memoryConfig["NLog:Rules:0:minLevel"] = "Trace";
memoryConfig["NLog:Rules:0:writeTo"] = "File,Console";
memoryConfig["NLog:Targets:console:type"] = "Console";
memoryConfig[$"{sectionName}:Rules:0:logger"] = "*";
memoryConfig[$"{sectionName}:Rules:0:minLevel"] = "Trace";
memoryConfig[$"{sectionName}:Rules:0:writeTo"] = "File,Console";
memoryConfig[$"{sectionName}:Targets:console:type"] = "Console";

return memoryConfig;
}
Expand Down

0 comments on commit a047ac1

Please sign in to comment.