Skip to content

Commit

Permalink
NLogAspNetCoreOptions - Added ReplaceLoggerFactory and RemoveLoggerFa…
Browse files Browse the repository at this point in the history
…ctoryFilter (#657)
  • Loading branch information
snakefoot authored Mar 28, 2021
1 parent 1df472e commit 4430692
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILoggerProvider, NLogLoggerProvider>(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<ILoggerFactory, NLogLoggerFactory>(serviceProvider => new NLogLoggerFactory(sharedFactory(serviceProvider, configuration, options))));
}

services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(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<NLogLoggerProvider>(null, Microsoft.Extensions.Logging.LogLevel.Trace));
}

//note: this one is called before services.AddSingleton<ILoggerFactory>
if ((options ?? NLogAspNetCoreOptions.Default).RegisterHttpContextAccessor)
Expand Down
18 changes: 17 additions & 1 deletion tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILoggerFactory>();
var loggerProvider = webhost.Services.GetService<ILoggerProvider>();

// 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()
Expand Down Expand Up @@ -235,7 +251,7 @@ private static ILoggerFactory GetLoggerFactory(IWebHost webhost)
return webhost.Services.GetService<Microsoft.Extensions.Logging.ILoggerFactory>();
}

#if !ASP_NET_CORE2 && !ASP_NET_CORE3
#if ASP_NET_CORE1
public class Startup
{
public Startup()
Expand Down

0 comments on commit 4430692

Please sign in to comment.