From 443069282603c4ad3723d1859c2bd0216facab79 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sun, 28 Mar 2021 22:28:46 +0200 Subject: [PATCH] NLogAspNetCoreOptions - Added ReplaceLoggerFactory and RemoveLoggerFactoryFilter (#657) --- src/NLog.Web.AspNetCore/AspNetExtensions.cs | 19 ++++++++++++++++++- .../AspNetCoreTests.cs | 18 +++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/NLog.Web.AspNetCore/AspNetExtensions.cs b/src/NLog.Web.AspNetCore/AspNetExtensions.cs index ac57f1a7..bd78f479 100644 --- a/src/NLog.Web.AspNetCore/AspNetExtensions.cs +++ b/src/NLog.Web.AspNetCore/AspNetExtensions.cs @@ -312,7 +312,24 @@ private static void AddNLogLoggerProvider(IServiceCollection services, IConfigur ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); - services.TryAddEnumerable(ServiceDescriptor.Singleton(serviceProvider => factory(serviceProvider, configuration, options))); + var sharedFactory = factory; + + if ((options ?? NLogAspNetCoreOptions.Default).ReplaceLoggerFactory) + { + NLogLoggerProvider singleInstance = null; // Ensure that registration of ILoggerFactory and ILoggerProvider shares the same single instance + sharedFactory = (provider, cfg, opt) => singleInstance ?? (singleInstance = factory(provider, cfg, opt)); + + services.AddLogging(builder => builder?.ClearProviders()); // Cleanup the existing LoggerFactory, before replacing it with NLogLoggerFactory + services.Replace(ServiceDescriptor.Singleton(serviceProvider => new NLogLoggerFactory(sharedFactory(serviceProvider, configuration, options)))); + } + + services.TryAddEnumerable(ServiceDescriptor.Singleton(serviceProvider => sharedFactory(serviceProvider, configuration, options))); + + if ((options ?? NLogAspNetCoreOptions.Default).RemoveLoggerFactoryFilter) + { + // Will forward all messages to NLog if not specifically overridden by user + services.AddLogging(builder => builder?.AddFilter(null, Microsoft.Extensions.Logging.LogLevel.Trace)); + } //note: this one is called before services.AddSingleton if ((options ?? NLogAspNetCoreOptions.Default).RegisterHttpContextAccessor) diff --git a/tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs b/tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs index 935fc0d3..0d8edf0c 100644 --- a/tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs +++ b/tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs @@ -54,6 +54,22 @@ public void UseNLogShouldLogTest() Assert.Equal("logger1|error1", logged.First()); } +#if !ASP_NET_CORE1 + [Fact] + public void UseNLog_ReplaceLoggerFactory() + { + var webhost = CreateWebHost(new NLogAspNetCoreOptions() { ReplaceLoggerFactory = true }); + + // Act + var loggerFactory = webhost.Services.GetService(); + var loggerProvider = webhost.Services.GetService(); + + // Assert + Assert.Equal(typeof(NLogLoggerFactory), loggerFactory.GetType()); + Assert.Equal(typeof(NLogLoggerProvider), loggerProvider.GetType()); + } +#endif + #if !ASP_NET_CORE1 && !ASP_NET_CORE2 [Fact] public void LoadConfigurationFromAppSettingsShouldLogTest() @@ -235,7 +251,7 @@ private static ILoggerFactory GetLoggerFactory(IWebHost webhost) return webhost.Services.GetService(); } -#if !ASP_NET_CORE2 && !ASP_NET_CORE3 +#if ASP_NET_CORE1 public class Startup { public Startup()