From f42beeb5b298e939846dedcd54beeb79c9b2cbba Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Wed, 25 Sep 2019 21:23:45 +0200 Subject: [PATCH 1/2] Introduce AddNLog with NLog-config parameter --- .../Extensions/ConfigureExtensions.cs | 26 +++++++++++++++++++ .../Extensions/ConfigureExtensionsTests.cs | 4 +-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs index a42e0165..840b39ce 100644 --- a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs +++ b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs @@ -97,6 +97,32 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProvider factory.Services.TryAddEnumerable(ServiceDescriptor.Singleton(s => new NLogLoggerProvider(options))); return factory; } + + /// + /// Enable NLog as logging provider for Microsoft Extension Logging + /// + /// + /// New NLog config. + /// + public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration config) + { + builder.AddNLog(); + LogManager.Configuration = config; + return builder; + } + + /// + /// Enable NLog as logging provider for Microsoft Extension Logging + /// + /// + /// relative path to NLog configuration file. + /// + public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string configFileRelativePath) + { + builder.AddNLog(); + LogManager.LoadConfiguration(configFileRelativePath); + return builder; + } #endif /// diff --git a/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs b/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs index 39a2b8f9..3de275fb 100644 --- a/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs +++ b/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs @@ -61,7 +61,7 @@ public void AddNLog_LogginBuilder_LogInfo_ShouldLogToNLog() // Act builder.AddNLog(); - LogManager.Configuration = config; + builder.AddNLog(config); var provider = GetLoggerProvider(builder); var logger = provider.CreateLogger("logger1"); @@ -84,7 +84,7 @@ internal class LoggingBuilderStub : ILoggingBuilder public IServiceCollection Services { get; set; } = new ServiceCollection(); } #endif - + private static void AssertSingleMessage(MemoryTarget memoryTarget, string expectedMessage) { Assert.Equal(1, memoryTarget.Logs.Count); From 4d82408dd3223d85233e59f7e46893b666f8c252 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sat, 28 Sep 2019 00:29:52 +0200 Subject: [PATCH 2/2] Fixed AddNLog with NLog-config parameter to initialize ConfigSettingLayoutRenderer first --- .../Extensions/ConfigureExtensions.cs | 41 ++++++++-------- .../Extensions/ConfigureExtensions.cs | 48 ++++++++++++------- .../Extensions/ConfigureExtensionsTests.cs | 1 - 3 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs b/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs index cf6a61c9..ae341157 100644 --- a/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs +++ b/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Reflection; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; @@ -36,30 +37,28 @@ public static IHostBuilder UseNLog(this IHostBuilder builder) public static IHostBuilder UseNLog(this IHostBuilder builder, NLogProviderOptions options) { if (builder == null) throw new ArgumentNullException(nameof(builder)); + builder.ConfigureServices((builderContext, services) => AddNLogLoggerProvider(services, builderContext.Configuration, options, CreateNLogLoggerProvider)); + return builder; + } - builder.ConfigureServices((hostbuilder, services) => - { - ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(ConfigureExtensions).GetTypeInfo() - .Assembly); - - if (hostbuilder.Configuration != null) - ConfigSettingLayoutRenderer.DefaultConfiguration = hostbuilder.Configuration; + private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, NLogProviderOptions options, Func factory) + { + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly); + services.Replace(ServiceDescriptor.Singleton(serviceProvider => factory(serviceProvider, configuration, options))); + } - // Try adding Singleton-implementation if not already exists - services.TryAddEnumerable(ServiceDescriptor.Singleton(serviceProvider => + private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options) + { + NLogLoggerProvider provider = new NLogLoggerProvider(options); + if (configuration != null) + { + ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; + if (options == null) { - var provider = new NLogLoggerProvider(options ?? new NLogProviderOptions()); - if (hostbuilder.Configuration != null) - { - ConfigSettingLayoutRenderer.DefaultConfiguration = hostbuilder.Configuration; - if (options == null) - provider.Configure(hostbuilder.Configuration.GetSection("Logging:NLog")); - } - return provider; - })); - }); - - return builder; + provider.Configure(configuration.GetSection("Logging:NLog")); + } + } + return provider; } } } \ No newline at end of file diff --git a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs index 840b39ce..e1de6f8d 100644 --- a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs +++ b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs @@ -56,7 +56,7 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOp #endif public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration configuration) { - var provider = CreateNLogProvider(configuration); + var provider = CreateNLogLoggerProvider(null, configuration, null); factory.AddProvider(provider); return factory; } @@ -69,7 +69,7 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration /// ILoggerFactory for chaining public static ILoggingBuilder AddNLog(this ILoggingBuilder factory) { - return factory.AddNLog(NLogProviderOptions.Default); + return factory.AddNLog((NLogProviderOptions)null); } /// @@ -80,8 +80,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory) /// ILoggerFactory for chaining public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfiguration configuration) { - // Try adding Singleton-implementation if not already exists - factory.Services.TryAddEnumerable(ServiceDescriptor.Singleton(s => CreateNLogProvider(configuration))); + AddNLogLoggerProvider(factory.Services, configuration, null, CreateNLogLoggerProvider); return factory; } @@ -93,8 +92,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfigurati /// ILoggerFactory for chaining public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProviderOptions options) { - // Try adding Singleton-implementation if not already exists - factory.Services.TryAddEnumerable(ServiceDescriptor.Singleton(s => new NLogLoggerProvider(options))); + AddNLogLoggerProvider(factory.Services, null, options, CreateNLogLoggerProvider); return factory; } @@ -102,12 +100,17 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProvider /// Enable NLog as logging provider for Microsoft Extension Logging /// /// - /// New NLog config. + /// New NLog config. /// - public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration config) + public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration configuration) { - builder.AddNLog(); - LogManager.Configuration = config; + AddNLogLoggerProvider(builder.Services, null, null, (serviceProvider, config, options) => + { + var provider = CreateNLogLoggerProvider(serviceProvider, config, options); + // Delay initialization of targets until we have loaded config-settings + LogManager.Configuration = configuration; + return provider; + }); return builder; } @@ -119,10 +122,20 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfi /// public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string configFileRelativePath) { - builder.AddNLog(); - LogManager.LoadConfiguration(configFileRelativePath); + AddNLogLoggerProvider(builder.Services, null, null, (serviceProvider, config, options) => + { + var provider = CreateNLogLoggerProvider(serviceProvider, config, options); + // Delay initialization of targets until we have loaded config-settings + LogManager.LoadConfiguration(configFileRelativePath); + return provider; + }); return builder; } + + private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, NLogProviderOptions options, Func factory) + { + services.TryAddEnumerable(ServiceDescriptor.Singleton(serviceProvider => factory(serviceProvider, configuration, options))); + } #endif /// @@ -183,15 +196,18 @@ public static NLogLoggerProvider Configure(this NLogLoggerProvider nlogProvider, return nlogProvider; } - private static NLogLoggerProvider CreateNLogProvider(IConfiguration configuration) + private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options) { - var provider = new NLogLoggerProvider(new NLogProviderOptions()); + NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogProviderOptions.Default); + configuration = configuration ?? (serviceProvider?.GetService(typeof(IConfiguration)) as IConfiguration); if (configuration != null) { ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; - provider.Configure(configuration.GetSection("Logging:NLog")); + if (options == null) + { + provider.Configure(configuration.GetSection("Logging:NLog")); + } } - return provider; } } diff --git a/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs b/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs index 3de275fb..b41e260a 100644 --- a/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs +++ b/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs @@ -60,7 +60,6 @@ public void AddNLog_LogginBuilder_LogInfo_ShouldLogToNLog() var config = CreateConfigWithMemoryTarget(out var memoryTarget); // Act - builder.AddNLog(); builder.AddNLog(config); var provider = GetLoggerProvider(builder); var logger = provider.CreateLogger("logger1");