-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load NLog config from appsettings.json using NLogLoggingConfiguration
- Loading branch information
Showing
13 changed files
with
282 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.Configuration; | ||
using NLog.Config; | ||
|
||
namespace NLog.Extensions.Logging | ||
{ | ||
/// <summary> | ||
/// Configures NLog through Microsoft Extension Configuration section (Ex from appsettings.json) | ||
/// </summary> | ||
public class NLogLoggingConfiguration : LoggingConfigurationParser | ||
{ | ||
private readonly Action<Object> _reloadConfiguration; | ||
|
||
private class LoggingConfigurationElement : ILoggingConfigurationElement | ||
{ | ||
readonly IConfigurationSection _configurationSection; | ||
readonly string _nameOverride; | ||
|
||
public bool AutoReload { get; private set; } | ||
|
||
public LoggingConfigurationElement(IConfigurationSection configurationSection, bool topElement, string nameOverride = null) | ||
{ | ||
_configurationSection = configurationSection; | ||
_nameOverride = nameOverride; | ||
if (topElement && bool.TryParse(configurationSection["autoreload"], out bool autoreload)) | ||
{ | ||
AutoReload = autoreload; | ||
} | ||
} | ||
|
||
public string Name => _nameOverride ?? _configurationSection.Key; | ||
|
||
public IEnumerable<KeyValuePair<string, string>> Values | ||
{ | ||
get | ||
{ | ||
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); | ||
} | ||
} | ||
|
||
public IEnumerable<ILoggingConfigurationElement> Children | ||
{ | ||
get | ||
{ | ||
var children = _configurationSection.GetChildren(); | ||
foreach (var child in children) | ||
{ | ||
var firstChildValue = child?.GetChildren()?.FirstOrDefault(); | ||
if (firstChildValue != null) | ||
{ | ||
if (_nameOverride == "target" && string.Equals(child.Key, "target", StringComparison.OrdinalIgnoreCase) && child.GetChildren().Count() == 1) | ||
{ | ||
yield return new LoggingConfigurationElement(firstChildValue, false, "target"); | ||
} | ||
else | ||
{ | ||
string nameOverride = null; | ||
if (string.Equals(_configurationSection.Key, "targets", StringComparison.OrdinalIgnoreCase)) | ||
nameOverride = "target"; | ||
yield return new LoggingConfigurationElement(child, false, nameOverride); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NLogLoggingConfiguration"/> class. | ||
/// </summary> | ||
/// <param name="nlogConfig">Configuration section to be read</param> | ||
public NLogLoggingConfiguration(IConfigurationSection nlogConfig) | ||
: this(nlogConfig, NLog.LogManager.LogFactory) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NLogLoggingConfiguration"/> class. | ||
/// </summary> | ||
/// <param name="nlogConfig">Configuration section to be read</param> | ||
/// <param name="logFactory">The <see cref="LogFactory"/> to which to apply any applicable configuration values.</param> | ||
public NLogLoggingConfiguration(IConfigurationSection nlogConfig, LogFactory logFactory) | ||
: base(logFactory) | ||
{ | ||
_reloadConfiguration = (state) => LoadConfigurationSection((IConfigurationSection)state, true); | ||
LoadConfigurationSection(nlogConfig, null); | ||
} | ||
|
||
private void LoadConfigurationSection(IConfigurationSection nlogConfig, bool? autoReload) | ||
{ | ||
var configElement = new LoggingConfigurationElement(nlogConfig, true); | ||
LoadConfig(configElement, null); | ||
if (autoReload ?? configElement.AutoReload) | ||
{ | ||
nlogConfig.GetReloadToken().RegisterChangeCallback(_reloadConfiguration, nlogConfig); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.