From 122f4e7bbc443c7c5ebacd2e84a08b1d77a593bb Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Tue, 17 May 2022 09:14:08 +0200 Subject: [PATCH] NLog 5.0 RTM (#777) --- CHANGELOG.MD | 31 +++++++++++++--- build.ps1 | 2 +- .../ASP.NET 4.6.1 - VS2017/packages.config | 2 +- .../ASP.NET Core 6 NLog Example.csproj | 1 + src/NLog.Web.AspNetCore/AspNetExtensions.cs | 35 +++++++++++-------- .../NLog.Web.AspNetCore.csproj | 4 +-- src/NLog.Web/NLog.Web.csproj | 2 +- .../AspNetCoreTests.cs | 20 +++++++++++ 8 files changed, 74 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 83abfef8..386803d3 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -4,6 +4,33 @@ See also [releases](https://github.com/NLog/NLog.Web/releases) and [milestones]( Date format: (year/month/day) +### v5.0 (2022/05/17) + +- **NLog.Web.AspNetCore** + - [#749](https://github.com/NLog/NLog.Web/pull/749) Include ContentRootPath when scanning for NLog.config candidate (#749) (@snakefoot) + - [#747](https://github.com/NLog/NLog.Web/pull/747) NLogRequestLogging - Added DurationThresholdMs + ExcludeRequestPaths (#747) (@snakefoot) + - [#734](https://github.com/NLog/NLog.Web/pull/734) LogManager.Setup() - Added support for RegisterAspNetLayoutRenderer (#734) (@snakefoot) + - [#735](https://github.com/NLog/NLog.Web/pull/735) Changed format Culture to InvariantCulture to match default NLog Culture (#735) (@snakefoot) + - [#736](https://github.com/NLog/NLog.Web/pull/736) Added sample for ASP.NET Core 6.0 on .NET 6 (#736) (@ThomasArdal) + - [#740](https://github.com/NLog/NLog.Web/pull/740) Added ${aspnet-request-routeparameters} (#740) (@ThomasArdal) + - [#741](https://github.com/NLog/NLog.Web/pull/741) Changed from Convert.ToString to IValueFormatter to be like NLog default (#741) (@snakefoot) + - [#701](https://github.com/NLog/NLog.Web/pull/701) Replaced platform NetCoreApp3.0 with NetCoreApp3.1 and added Net5.0 (#701) (@snakefoot) + - [#683](https://github.com/NLog/NLog.Web/pull/683) Dropped support for ASP.NET Core 1 and NetStandard1.5 + Net452 (#683) (@304NotModified) + - [#691](https://github.com/NLog/NLog.Web/pull/691) Enabled ShutdownOnDispose by default to follow lifetime of the ServiceProvider (#691) (@snakefoot) + - [#700](https://github.com/NLog/NLog.Web/pull/700) Added W3CExtendedLogLayout for writing W3C Extended Logs (#700) (@snakefoot) + - [#697](https://github.com/NLog/NLog.Web/pull/697) Replaced SingleAsArray with OutputFormat = JsonArray + JsonDictionary (#697) (@snakefoot) + +- **NLog.Web** + - [#734](https://github.com/NLog/NLog.Web/pull/734) LogManager.Setup() - Added support for RegisterNLogWeb + RegisterAspNetLayoutRenderer (#734) (@snakefoot) + - [#735](https://github.com/NLog/NLog.Web/pull/735) Changed format Culture to InvariantCulture to match default NLog Culture (#735) (@snakefoot) + - [#740](https://github.com/NLog/NLog.Web/pull/740) Added ${aspnet-request-routeparameters} (#740) (@ThomasArdal) + - [#741](https://github.com/NLog/NLog.Web/pull/741) Changed from Convert.ToString to IValueFormatter to be like NLog default (#741) (@snakefoot) + - [#701](https://github.com/NLog/NLog.Web/pull/701) Added platform Net46 (#701) (@snakefoot) + - [#700](https://github.com/NLog/NLog.Web/pull/700) Added W3CExtendedLogLayout for writing W3C Extended Logs (#700) (@snakefoot) + - [#697](https://github.com/NLog/NLog.Web/pull/697) Replaced SingleAsArray with OutputFormat = JsonArray + JsonDictionary (#697) (@snakefoot) + +See also [List of major changes in NLog 5](https://nlog-project.org/2021/08/25/nlog-5-0-preview1-ready.html) + ### v5.0-rc2 (2022/01/19) - **NLog.Web.AspNetCore** @@ -25,8 +52,6 @@ Date format: (year/month/day) - [#740](https://github.com/NLog/NLog.Web/pull/740) Added ${aspnet-request-routeparameters} (#740) (@ThomasArdal) - [#741](https://github.com/NLog/NLog.Web/pull/741) Changed from Convert.ToString to IValueFormatter to be like NLog default (#741) (@snakefoot) -See also [Release post for NLog 5](https://nlog-project.org/2021/08/25/nlog-5-0-preview1-ready.html) - ### v5.0-preview 3 (2021/10/28) - **NLog.Web.AspNetCore** @@ -41,8 +66,6 @@ See also [Release post for NLog 5](https://nlog-project.org/2021/08/25/nlog-5-0- - [#700](https://github.com/NLog/NLog.Web/pull/700) Added W3CExtendedLogLayout for writing W3C Extended Logs (#700) (@snakefoot) - [#697](https://github.com/NLog/NLog.Web/pull/697) Replaced SingleAsArray with OutputFormat = JsonArray + JsonDictionary (#697) (@snakefoot) -See also [Release post for NLog 5](https://nlog-project.org/2021/08/25/nlog-5-0-preview1-ready.html) - ### v4.14 (2021/08/28) - **NLog.Web.AspNetCore** - [#677](https://github.com/NLog/NLog.Web/pull/677) Bump NLog.Extensions.Logging from 1.7.3 to 1.7.4 (#677) (@dependabot[bot]) diff --git a/build.ps1 b/build.ps1 index bd0cfc48..519c1eca 100644 --- a/build.ps1 +++ b/build.ps1 @@ -3,7 +3,7 @@ dotnet --version $versionPrefix = "5.0" # Also update version for minor versions in appveyor.yml -$versionSuffix = "rc2" +$versionSuffix = "" $versionFile = $versionPrefix + "." + ${env:APPVEYOR_BUILD_NUMBER} if ($env:APPVEYOR_PULL_REQUEST_NUMBER) { $versionPrefix = $versionFile diff --git a/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/packages.config b/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/packages.config index 6ea95662..7aea3e62 100644 --- a/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/packages.config +++ b/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/packages.config @@ -12,7 +12,7 @@ - + \ No newline at end of file diff --git a/examples/ASP.NET Core 6/ASP.NET Core 6 NLog Example/ASP.NET Core 6 NLog Example.csproj b/examples/ASP.NET Core 6/ASP.NET Core 6 NLog Example/ASP.NET Core 6 NLog Example.csproj index 80f24716..119c5e54 100644 --- a/examples/ASP.NET Core 6/ASP.NET Core 6 NLog Example/ASP.NET Core 6 NLog Example.csproj +++ b/examples/ASP.NET Core 6/ASP.NET Core 6 NLog Example/ASP.NET Core 6 NLog Example.csproj @@ -10,6 +10,7 @@ + diff --git a/src/NLog.Web.AspNetCore/AspNetExtensions.cs b/src/NLog.Web.AspNetCore/AspNetExtensions.cs index 539d0df1..c3928043 100644 --- a/src/NLog.Web.AspNetCore/AspNetExtensions.cs +++ b/src/NLog.Web.AspNetCore/AspNetExtensions.cs @@ -227,7 +227,7 @@ public static ILoggingBuilder AddNLogWeb(this ILoggingBuilder builder, Func { - config = SetupConfiguration(serviceProvider, config); + config = SetupNLogConfigSettings(serviceProvider, config); // Delay initialization of targets until we have loaded config-settings var logFactory = factoryBuilder(serviceProvider); var provider = CreateNLogLoggerProvider(serviceProvider, config, env, options, logFactory); @@ -301,47 +301,55 @@ public static IHostBuilder UseNLog(this IHostBuilder builder, NLogAspNetCoreOpti return builder; } - private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, IHostEnvironment hostEnvironment, NLogAspNetCoreOptions options, Func factory) + private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration hostConfiguration, IHostEnvironment hostEnvironment, NLogAspNetCoreOptions options, Func factory) { ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); + options = options ?? NLogAspNetCoreOptions.Default; + options.Configure(hostConfiguration?.GetSection("Logging:NLog")); + var sharedFactory = factory; - if ((options ?? NLogAspNetCoreOptions.Default).ReplaceLoggerFactory) + if (options.ReplaceLoggerFactory) { NLogLoggerProvider singleInstance = null; // Ensure that registration of ILoggerFactory and ILoggerProvider shares the same single instance sharedFactory = (provider, cfg, env, opt) => singleInstance ?? (singleInstance = factory(provider, cfg, env, 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, hostEnvironment, options)))); + services.Replace(ServiceDescriptor.Singleton(serviceProvider => new NLogLoggerFactory(sharedFactory(serviceProvider, hostConfiguration, hostEnvironment, options)))); } - services.TryAddEnumerable(ServiceDescriptor.Singleton(serviceProvider => sharedFactory(serviceProvider, configuration, hostEnvironment, options))); + services.TryAddEnumerable(ServiceDescriptor.Singleton(serviceProvider => sharedFactory(serviceProvider, hostConfiguration, hostEnvironment, options))); - if ((options ?? NLogAspNetCoreOptions.Default).RemoveLoggerFactoryFilter) + if (options.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) + if (options.RegisterHttpContextAccessor) { services.AddHttpContextAccessor(); } } - private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, IHostEnvironment hostEnvironment, NLogAspNetCoreOptions options) + private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration hostConfiguration, IHostEnvironment hostEnvironment, NLogAspNetCoreOptions options) { - return CreateNLogLoggerProvider(serviceProvider, configuration, hostEnvironment, options, null); + return CreateNLogLoggerProvider(serviceProvider, hostConfiguration, hostEnvironment, options, null); } - private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, IHostEnvironment hostEnvironment, NLogAspNetCoreOptions options, NLog.LogFactory logFactory) + private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration hostConfiguration, IHostEnvironment hostEnvironment, NLogAspNetCoreOptions options, NLog.LogFactory logFactory) { - NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogAspNetCoreOptions.Default, logFactory ?? LogManager.LogFactory); + NLogLoggerProvider provider = new NLogLoggerProvider(options, logFactory ?? LogManager.LogFactory); + + var configuration = SetupNLogConfigSettings(serviceProvider, hostConfiguration); - configuration = SetupConfiguration(serviceProvider, configuration); + if (configuration != null && (!ReferenceEquals(configuration, hostConfiguration) || options == null)) + { + provider.Configure(configuration.GetSection("Logging:NLog")); + } if (serviceProvider != null && provider.Options.RegisterServiceProvider) { @@ -350,7 +358,6 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv if (configuration != null) { - provider.Configure(configuration.GetSection("Logging:NLog")); TryLoadConfigurationFromSection(provider, configuration); } @@ -395,7 +402,7 @@ private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactor }); } - private static IConfiguration SetupConfiguration(IServiceProvider serviceProvider, IConfiguration configuration) + private static IConfiguration SetupNLogConfigSettings(IServiceProvider serviceProvider, IConfiguration configuration) { ServiceLocator.ServiceProvider = serviceProvider; configuration = configuration ?? (serviceProvider?.GetService(typeof(IConfiguration)) as IConfiguration); diff --git a/src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj b/src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj index e468818c..44160cbd 100644 --- a/src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj +++ b/src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj @@ -10,7 +10,7 @@ Supported platforms: - For ASP.NET Core 6, .NET 6 - For ASP.NET Core 5, .NET 5 -- For ASP.NET Core 3, .NET Core 3.0 +- For ASP.NET Core 3, .NET Core 3.1 - For ASP.NET Core 2, .NET Standard 2.0+ and .NET 4.6+ Julian Verdurmen @@ -75,7 +75,7 @@ NLog 5 release post: https://nlog-project.org/2021/08/25/nlog-5-0-preview1-ready $(DefineConstants);ASP_NET_CORE;ASP_NET_CORE3 - + diff --git a/src/NLog.Web/NLog.Web.csproj b/src/NLog.Web/NLog.Web.csproj index 25e9886f..d1f369bb 100644 --- a/src/NLog.Web/NLog.Web.csproj +++ b/src/NLog.Web/NLog.Web.csproj @@ -49,7 +49,7 @@ See https://github.com/NLog/NLog.Web/releases - + diff --git a/tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs b/tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs index 36962f5b..362b50c6 100644 --- a/tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs +++ b/tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs @@ -248,6 +248,26 @@ public void UseNLog_LoadConfigurationFromSection() Assert.Equal("logger1|error1|Memory", logged[0]); } + [Fact] + public void UseNLog_ReplaceLoggerFactory_FromConfiguration() + { + var host = CreateWebHostBuilder().ConfigureAppConfiguration((context, config) => + { + var memoryConfig = new Dictionary(); + memoryConfig["Logging:NLog:ReplaceLoggerFactory"] = "True"; + memoryConfig["Logging:NLog:RemoveLoggerFactoryFilter"] = "False"; + config.AddInMemoryCollection(memoryConfig); + }).UseNLog().Build(); + + // Act + var loggerFactory = host.Services.GetService(); + var loggerProvider = host.Services.GetService(); + + // Assert + Assert.Equal(typeof(NLogLoggerFactory), loggerFactory.GetType()); + Assert.Equal(typeof(NLogLoggerProvider), loggerProvider.GetType()); + } + private static IWebHostBuilder CreateWebHostBuilder() { var builder =