From 73588c5833381da3de7f2ca3f331dee63f4552a1 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Thu, 17 Jan 2019 00:46:58 +0100 Subject: [PATCH] Register WebHost configuration with ${configsetting} and LoggingProvider-options --- .../NLog.Web.AspNetCore.Tests.csproj | 2 +- NLog.Web.AspNetCore/AspNetExtensions.cs | 67 ++++++++++++++----- .../NLog.Web.AspNetCore.csproj | 18 ++--- appveyor.yml | 8 +-- 4 files changed, 66 insertions(+), 29 deletions(-) diff --git a/NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj b/NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj index 82e4e5f04..b9d81fc58 100644 --- a/NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj +++ b/NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj @@ -33,7 +33,7 @@ - + diff --git a/NLog.Web.AspNetCore/AspNetExtensions.cs b/NLog.Web.AspNetCore/AspNetExtensions.cs index c31b0afb4..18b1f771a 100644 --- a/NLog.Web.AspNetCore/AspNetExtensions.cs +++ b/NLog.Web.AspNetCore/AspNetExtensions.cs @@ -4,14 +4,14 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Logging; using NLog.Config; using NLog.Extensions.Logging; using NLog.Web.DependencyInjection; #if ASP_NET_CORE2 using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Logging; #endif namespace NLog.Web @@ -100,27 +100,64 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder) public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCoreOptions options) { if (builder == null) throw new ArgumentNullException(nameof(builder)); - options = options ?? NLogAspNetCoreOptions.Default; builder.ConfigureServices(services => { - ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); - LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + ConfigureServicesNLog(options, services, (serviceProvider) => serviceProvider.GetService()); + }); + return builder; + } - //note: when registering ILoggerFactory, all non NLog stuff and stuff before this will be removed - services.AddSingleton(serviceProvider => - { - ServiceLocator.ServiceProvider = serviceProvider; - return new NLogLoggerProvider(options); - }); + /// + /// Use NLog for Dependency Injected loggers. + /// + public static Microsoft.Extensions.Hosting.IHostBuilder UseNLog(this Microsoft.Extensions.Hosting.IHostBuilder builder) + { + return UseNLog(builder, null); + } - //note: this one is called before services.AddSingleton - if (options.RegisterHttpContextAccessor) + /// + /// Use NLog for Dependency Injected loggers. + /// + /// + /// Options for logging to NLog with Dependency Injected loggers + /// + public static Microsoft.Extensions.Hosting.IHostBuilder UseNLog(this Microsoft.Extensions.Hosting.IHostBuilder builder, NLogAspNetCoreOptions options) + { + if (builder == null) throw new ArgumentNullException(nameof(builder)); + + builder.ConfigureServices((hostbuilder, services) => + { + ConfigureServicesNLog(options, services, (serviceProvider) => hostbuilder.Configuration); + }); + return builder; + } + + private static void ConfigureServicesNLog(NLogAspNetCoreOptions options, IServiceCollection services, Func lookupConfiguration) + { + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + + services.AddSingleton(serviceProvider => + { + ServiceLocator.ServiceProvider = serviceProvider; + + var provider = new NLogLoggerProvider(options ?? new NLogProviderOptions()); + var configuration = lookupConfiguration(serviceProvider); + if (configuration != null) { - services.TryAddSingleton(); + ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; + if (options == null) + provider.Configure(configuration.GetSection("Logging:NLog")); } + return provider; }); - return builder; + + //note: this one is called before services.AddSingleton + if ((options ?? NLogAspNetCoreOptions.Default).RegisterHttpContextAccessor) + { + services.TryAddSingleton(); + } } #endif diff --git a/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj b/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj index fd7d2ddf0..510deb0e6 100644 --- a/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj +++ b/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj @@ -5,7 +5,7 @@ netstandard1.5;net451;net461;netstandard2.0 - 4.7.1 + 4.8 $(VersionPrefix)$(VersionSuffix) $(Version) @@ -30,10 +30,10 @@ Supported platforms: NLog.Web.AspNetCore logging;log;session;NLog;web;aspnet;aspnetcore;MVC;httpcontext -- Added ThreadSafe-attribute for LayoutRenderer to optimize async Precalculate (@snakefoot) -- ${AspNetRequestIp} added CheckForwardedForHeader to check for X-Forwarded-For header (#325) (@Giorgi) -- Check that Content-Type is correct before accessing the Form (#322) (@mcliment) -- Enabled SymbolPackageFormat=snupkg (@snakefoot) +- Updated Microsoft.AspNetCore to ver. 2.1 (Long-Term-Support) +- ${configsetting} layout renderer now depends on UseNLog-call to provide IConfiguration using IServiceProvider +- NLogLoggerProvider options now binds to IConfiguration (Using section Logging:NLog) +- UseNLog-call now also available for IHostBuilder http://nlog-project.org/N.png https://github.com/NLog/NLog.Web @@ -74,7 +74,7 @@ Supported platforms: - + @@ -89,9 +89,9 @@ Supported platforms: - - - + + + diff --git a/appveyor.yml b/appveyor.yml index 09302129d..68d333c38 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: 4.7.1.{build} +version: 4.8.0.{build} clone_folder: c:\projects\nlogweb configuration: Release image: Visual Studio 2017 @@ -7,8 +7,8 @@ assembly_info: patch: true file: '**\AssemblyInfo.cs' assembly_version: '4.0.0' - assembly_file_version: '4.7.1.{build}' #NLog.Web - assembly_informational_version: '4.7.1' #NLog.Web + assembly_file_version: '4.8.0.{build}' #NLog.Web + assembly_informational_version: '4.8.0' #NLog.Web nuget: project_feed: true matrix: @@ -22,7 +22,7 @@ skip_tags: true build_script: - cmd: >- - call build_aspnet.bat -nuget_version=4.7.1 # NLog.Web package + call build_aspnet.bat -nuget_version=4.8.0 # NLog.Web package call build_aspnetcore.bat # update NLog.Web.AspNetCore.csproj for version number