diff --git a/src/NLog.Extensions.Logging/Config/ISetupLoggerProviderBuilder.cs b/src/NLog.Extensions.Logging/Config/ISetupLoggerProviderBuilder.cs deleted file mode 100644 index 6840f993..00000000 --- a/src/NLog.Extensions.Logging/Config/ISetupLoggerProviderBuilder.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace NLog.Extensions.Logging -{ - /// - /// Interface for fluent setup of LogFactory integration with Microsoft Extension Logging - /// - public interface ISetupExtensionLoggingBuilder - { - /// - /// LogFactory under configuration - /// - LogFactory LogFactory { get; } - } -} diff --git a/src/NLog.Extensions.Logging/Config/SetupBuilderExtensions.cs b/src/NLog.Extensions.Logging/Config/SetupBuilderExtensions.cs index ed6985c4..e2a693e4 100644 --- a/src/NLog.Extensions.Logging/Config/SetupBuilderExtensions.cs +++ b/src/NLog.Extensions.Logging/Config/SetupBuilderExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System.Linq; using NLog.Config; namespace NLog.Extensions.Logging @@ -9,22 +9,17 @@ namespace NLog.Extensions.Logging public static class SetupBuilderExtensions { /// - /// Setup of LogFactory integration with Microsoft Extension Logging + /// Loads NLog LoggingConfiguration from appsettings.json from NLog-section /// - public static ISetupBuilder SetupExtensionLogging(this ISetupBuilder setupBuilder, Action extensionsBuilder) + public static ISetupBuilder LoadNLogConfigFromSection(this ISetupBuilder setupBuilder, Microsoft.Extensions.Configuration.IConfiguration configuration) { - extensionsBuilder(new SetupExtensionLoggingBuilder(setupBuilder.LogFactory)); - return setupBuilder; - } - - private class SetupExtensionLoggingBuilder : ISetupExtensionLoggingBuilder - { - public SetupExtensionLoggingBuilder(LogFactory logFactory) + setupBuilder.SetupExtensions(s => s.RegisterConfigSettings(configuration)); + var nlogConfig = configuration.GetSection("NLog"); + if (nlogConfig != null && nlogConfig.GetChildren().Any()) { - LogFactory = logFactory; + setupBuilder.LogFactory.Configuration = new NLogLoggingConfiguration(nlogConfig, setupBuilder.LogFactory); } - - public LogFactory LogFactory { get; } + return setupBuilder; } } } diff --git a/src/NLog.Extensions.Logging/Config/SetupExtensionsBuilderExtensions.cs b/src/NLog.Extensions.Logging/Config/SetupExtensionsBuilderExtensions.cs new file mode 100644 index 00000000..46056a74 --- /dev/null +++ b/src/NLog.Extensions.Logging/Config/SetupExtensionsBuilderExtensions.cs @@ -0,0 +1,21 @@ +using Microsoft.Extensions.Configuration; +using NLog.Config; + +namespace NLog.Extensions.Logging +{ + /// + /// Extension methods to setup NLog extensions, so they are known when loading NLog LoggingConfiguration + /// + public static class SetupExtensionsBuilderExtensions + { + /// + /// Replace with version from NLog.Extension.Logging when it has been released with NLog 4.7 + /// + internal static ISetupExtensionsBuilder RegisterConfigSettings(this ISetupExtensionsBuilder setupBuilder, IConfiguration configuration) + { + ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; + ConfigurationItemFactory.Default.RegisterType(typeof(ConfigSettingLayoutRenderer), string.Empty); + return setupBuilder; + } + } +} diff --git a/src/NLog.Extensions.Logging/Config/SetupLoggerProviderBuilderExtensions.cs b/src/NLog.Extensions.Logging/Config/SetupLoggerProviderBuilderExtensions.cs deleted file mode 100644 index ab655492..00000000 --- a/src/NLog.Extensions.Logging/Config/SetupLoggerProviderBuilderExtensions.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System.Reflection; - -namespace NLog.Extensions.Logging -{ - /// - /// Extension methods to setup NLog together with Microsoft Extension Logging - /// - public static class SetupLoggerProviderBuilderExtensions - { - /// - /// Enables lookup of settings from appsettings.json using ${configsetting} - /// - public static ISetupExtensionLoggingBuilder SetupConfigSettings(this ISetupExtensionLoggingBuilder setupBuilder, Microsoft.Extensions.Configuration.IConfiguration configuration) - { - ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; - NLog.Config.ConfigurationItemFactory.Default.RegisterType(typeof(ConfigSettingLayoutRenderer), string.Empty); - return setupBuilder; - } - - /// - /// Loads NLog LoggingConfiguration from appsettings.json from NLog-section - /// - public static ISetupExtensionLoggingBuilder LoadNLogLoggingConfiguration(this ISetupExtensionLoggingBuilder setupBuilder, Microsoft.Extensions.Configuration.IConfiguration configuration) - { - SetupConfigSettings(setupBuilder, configuration); - setupBuilder.LogFactory.Configuration = new NLogLoggingConfiguration(configuration.GetSection("NLog"), setupBuilder.LogFactory); - return setupBuilder; - } - } -}