-
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 JSON based NLog config (e.g. declared in appsettings.json) + Beg…
…inScopeParser performance improvements (#263) * Load NLog config from appsettings.json using NLogLoggingConfiguration * Update NLog.Extensions.Logging.csproj * refactor
- Loading branch information
1 parent
2460b35
commit d7717f4
Showing
15 changed files
with
365 additions
and
107 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
112 changes: 112 additions & 0 deletions
112
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,112 @@ | ||
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; | ||
|
||
/// <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, 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); | ||
} | ||
} | ||
|
||
private class LoggingConfigurationElement : ILoggingConfigurationElement | ||
{ | ||
readonly IConfigurationSection _configurationSection; | ||
readonly string _nameOverride; | ||
|
||
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)) | ||
{ | ||
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) | ||
{ | ||
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")) | ||
{ | ||
nameOverride = "target"; | ||
} | ||
yield return new LoggingConfigurationElement(child, false, nameOverride); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,13 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace NLog.Extensions.Logging | ||
{ | ||
internal static class StringExtensions | ||
{ | ||
internal static bool EqualsOrdinalIgnoreCase(this string text, string text2) | ||
{ | ||
return string.Equals(text, text2, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} | ||
} |
Oops, something went wrong.