From 20edaba84a18b1020f5c1973df18c5afba9e51e8 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Mon, 9 Apr 2018 21:25:58 +0200 Subject: [PATCH 1/2] Added public method to configure the NLog ServiceLocator --- NLog.Web.AspNetCore/AspNetExtensions.cs | 22 +++++++++++++++---- .../ServiceLocator.cs | 2 -- 2 files changed, 18 insertions(+), 6 deletions(-) rename NLog.Web.AspNetCore/{DependencyInjection => Internal}/ServiceLocator.cs (99%) diff --git a/NLog.Web.AspNetCore/AspNetExtensions.cs b/NLog.Web.AspNetCore/AspNetExtensions.cs index 6a6bcc39..0a86ae30 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(); } /// @@ -103,8 +101,9 @@ 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); + LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); builder.ConfigureServices(services => { @@ -125,5 +124,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 a834485f..630096a9 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; } - - } } From 77a0ec70dcd74c4f0c529d242420ea40a5693a4b Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Wed, 11 Apr 2018 07:15:12 +0200 Subject: [PATCH 2/2] Added public method to configure the NLog ServiceLocator (One assembly lookup) --- NLog.Web.AspNetCore/AspNetExtensions.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/NLog.Web.AspNetCore/AspNetExtensions.cs b/NLog.Web.AspNetCore/AspNetExtensions.cs index 0a86ae30..f63cab2e 100644 --- a/NLog.Web.AspNetCore/AspNetExtensions.cs +++ b/NLog.Web.AspNetCore/AspNetExtensions.cs @@ -21,6 +21,8 @@ namespace NLog.Web /// public static class AspNetExtensions { + private static readonly Assembly _nlogWebAssembly = typeof(AspNetExtensions).GetTypeInfo().Assembly; + /// /// Enable NLog Web for ASP.NET Core. /// @@ -41,8 +43,8 @@ public static void AddNLogWeb(this IApplicationBuilder app) /// LoggingConfiguration for chaining public static LoggingConfiguration ConfigureNLog(this IHostingEnvironment env, string configFileRelativePath) { - ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); - LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly); + LogManager.AddHiddenAssembly(_nlogWebAssembly); var fileName = Path.Combine(env.ContentRootPath, configFileRelativePath); LogManager.LoadConfiguration(fileName); return LogManager.Configuration; @@ -61,7 +63,7 @@ public static LoggingConfiguration ConfigureNLog(this IHostingEnvironment env, s [Obsolete("Use UseNLog() on IWebHostBuilder, and NLog.Web.NLogBuilder.ConfigureNLog()")] public static LogFactory ConfigureNLog(this ILoggingBuilder builder, string configFileName) { - ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly); builder.AddNLog(); return LogManager.LoadConfiguration(configFileName); } @@ -77,7 +79,7 @@ public static LogFactory ConfigureNLog(this ILoggingBuilder builder, string conf [Obsolete("Use UseNLog() on IWebHostBuilder, and NLog.Web.NLogBuilder.ConfigureNLog()")] public static LogFactory ConfigureNLog(this ILoggingBuilder builder, LoggingConfiguration configuration) { - ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly); builder.AddNLog(); LogManager.Configuration = configuration; return LogManager.LogFactory; @@ -102,8 +104,8 @@ 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); - LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly); + LogManager.AddHiddenAssembly(_nlogWebAssembly); builder.ConfigureServices(services => { @@ -135,8 +137,8 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo public static IServiceProvider SetupNLogServiceLocator(this IServiceProvider serviceProvider) { ServiceLocator.ServiceProvider = serviceProvider; - ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); - LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly); + LogManager.AddHiddenAssembly(_nlogWebAssembly); return serviceProvider; } }