Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NLog 5.0 RTM #777

Merged
merged 1 commit into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand All @@ -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**
Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.4" targetFramework="net461" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
<package id="Modernizr" version="2.8.3" targetFramework="net461" />
<package id="NLog" version="5.0.0-rc2" targetFramework="net461" />
<package id="NLog" version="5.0.0" targetFramework="net461" />
<package id="System.Diagnostics.DiagnosticSource" version="4.4.1" targetFramework="net461" />
<package id="WebGrease" version="1.6.0" targetFramework="net461" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<ProjectReference Include="..\..\..\src\NLog.Web.AspNetCore\NLog.Web.AspNetCore.csproj" />
<ProjectReference Include="..\..\ASP.NET Core 5\ASP.NET Core 5 NLog Example\ASP.NET Core 5 NLog Example.csproj" />
</ItemGroup>

</Project>
35 changes: 21 additions & 14 deletions src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static ILoggingBuilder AddNLogWeb(this ILoggingBuilder builder, Func<ISer
{
AddNLogLoggerProvider(builder.Services, null, null, null, (serviceProvider, config, env, options) =>
{
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);
Expand Down Expand Up @@ -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<IServiceProvider, IConfiguration, IHostEnvironment, NLogAspNetCoreOptions, NLogLoggerProvider> factory)
private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration hostConfiguration, IHostEnvironment hostEnvironment, NLogAspNetCoreOptions options, Func<IServiceProvider, IConfiguration, IHostEnvironment, NLogAspNetCoreOptions, NLogLoggerProvider> 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<ILoggerFactory, NLogLoggerFactory>(serviceProvider => new NLogLoggerFactory(sharedFactory(serviceProvider, configuration, hostEnvironment, options))));
services.Replace(ServiceDescriptor.Singleton<ILoggerFactory, NLogLoggerFactory>(serviceProvider => new NLogLoggerFactory(sharedFactory(serviceProvider, hostConfiguration, hostEnvironment, options))));
}

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

//note: this one is called before services.AddSingleton<ILoggerFactory>
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)
{
Expand All @@ -350,7 +358,6 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv

if (configuration != null)
{
provider.Configure(configuration.GetSection("Logging:NLog"));
TryLoadConfigurationFromSection(provider, configuration);
}

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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+
</Description>
<Authors>Julian Verdurmen</Authors>
Expand Down Expand Up @@ -75,7 +75,7 @@ NLog 5 release post: https://nlog-project.org/2021/08/25/nlog-5-0-preview1-ready
<DefineConstants>$(DefineConstants);ASP_NET_CORE;ASP_NET_CORE3</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog.Extensions.Logging" Version="5.0.0-rc2" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.0.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net461' ">
<!-- Fixed to 2.1.0 as 2.1 is Long Term Supported (LTS) and works with vanilla .NET Core 2.1 SDK -->
Expand Down
2 changes: 1 addition & 1 deletion src/NLog.Web/NLog.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ See https://github.com/NLog/NLog.Web/releases
<DownloadFile SourceUrl="https://nlog-project.org/N.png" DestinationFolder="$(MSBuildThisFileDirectory)" />
</Target>
<ItemGroup>
<PackageReference Include="NLog" Version="5.0.0-rc2" />
<PackageReference Include="NLog" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
Expand Down
20 changes: 20 additions & 0 deletions tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>();
memoryConfig["Logging:NLog:ReplaceLoggerFactory"] = "True";
memoryConfig["Logging:NLog:RemoveLoggerFactoryFilter"] = "False";
config.AddInMemoryCollection(memoryConfig);
}).UseNLog().Build();

// Act
var loggerFactory = host.Services.GetService<ILoggerFactory>();
var loggerProvider = host.Services.GetService<ILoggerProvider>();

// Assert
Assert.Equal(typeof(NLogLoggerFactory), loggerFactory.GetType());
Assert.Equal(typeof(NLogLoggerProvider), loggerProvider.GetType());
}

private static IWebHostBuilder CreateWebHostBuilder()
{
var builder =
Expand Down