diff --git a/NLog.Web.AspNetCore/AspNetExtensions.cs b/NLog.Web.AspNetCore/AspNetExtensions.cs index 6a6bcc393..cffcfb278 100644 --- a/NLog.Web.AspNetCore/AspNetExtensions.cs +++ b/NLog.Web.AspNetCore/AspNetExtensions.cs @@ -30,9 +30,7 @@ public static class AspNetExtensions #endif public static void AddNLogWeb(this IApplicationBuilder app) { - ServiceLocator.ServiceProvider = app.ApplicationServices; - ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); - LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + app.ApplicationServices.SetupNLogServiceLocator(); } /// @@ -104,14 +102,12 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo if (builder == null) throw new ArgumentNullException(nameof(builder)); options = options ?? NLogAspNetCoreOptions.Default; - ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); - builder.ConfigureServices(services => { //note: when registering ILoggerFactory, all non NLog stuff and stuff before this will be removed services.AddSingleton(serviceProvider => { - ServiceLocator.ServiceProvider = serviceProvider; + serviceProvider.SetupNLogServiceLocator(); return new NLogLoggerProvider(options); }); @@ -125,5 +121,20 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo } #endif + /// + /// Override the default used by the NLog ServiceLocator. + /// NLog ServiceLocator uses the to access context specific services (Ex. ) + /// + /// + /// Should only be used if the standard approach for configuring NLog is not enough + /// + /// + public static IServiceProvider SetupNLogServiceLocator(this IServiceProvider serviceProvider) + { + ServiceLocator.ServiceProvider = serviceProvider; + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + return serviceProvider; + } } } diff --git a/NLog.Web.AspNetCore/DependencyInjection/ServiceLocator.cs b/NLog.Web.AspNetCore/Internal/ServiceLocator.cs similarity index 99% rename from NLog.Web.AspNetCore/DependencyInjection/ServiceLocator.cs rename to NLog.Web.AspNetCore/Internal/ServiceLocator.cs index a834485f8..630096a9a 100644 --- a/NLog.Web.AspNetCore/DependencyInjection/ServiceLocator.cs +++ b/NLog.Web.AspNetCore/Internal/ServiceLocator.cs @@ -16,7 +16,5 @@ internal static class ServiceLocator /// The current service provider for reading ASP.NET Core session, request etc. /// public static IServiceProvider ServiceProvider { get; set; } - - } }