Skip to content

Commit

Permalink
NLogProviderOptions with support for RegisterServiceProvider (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Oct 28, 2021
1 parent f0b9110 commit 08b4eeb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

# NLog.Web (ASP.NET & ASP.NET Core)

[![NuGet Release](https://img.shields.io/nuget/v/NLog.Extensions.Logging.svg?label=NLog.Web.AspNetCore)](https://www.nuget.org/packages/NLog.Web.AspNetCore)
[![NuGet Pre Release](https://img.shields.io/nuget/vpre/NLog.Extensions.Logging.svg?label=NLog.Web.AspNetCore)](https://www.nuget.org/packages/NLog.Web.AspNetCore)
[![NuGet Release](https://img.shields.io/nuget/v/NLog.Web.AspNetCore.svg?label=NLog.Web.AspNetCore)](https://www.nuget.org/packages/NLog.Web.AspNetCore)
[![NuGet Pre Release](https://img.shields.io/nuget/vpre/NLog.Web.AspNetCore.svg?label=NLog.Web.AspNetCore)](https://www.nuget.org/packages/NLog.Web.AspNetCore)

[![NuGet Release](https://img.shields.io/nuget/v/NLog.Extensions.Hosting.svg?label=NLog.Web)](https://www.nuget.org/packages/NLog.Web)
[![NuGet Pre Release](https://img.shields.io/nuget/vpre/NLog.Extensions.Hosting.svg?label=NLog.Web)](https://www.nuget.org/packages/NLog.Web)
[![NuGet Release](https://img.shields.io/nuget/v/NLog.Web.svg?label=NLog.Web)](https://www.nuget.org/packages/NLog.Web)
[![NuGet Pre Release](https://img.shields.io/nuget/vpre/NLog.Web.svg?label=NLog.Web)](https://www.nuget.org/packages/NLog.Web)

[![Build status](https://img.shields.io/appveyor/ci/nlog/nlog-web/master.svg)](https://ci.appveyor.com/project/nlog/nlog-web/branch/master)
[![](https://sonarcloud.io/api/project_badges/measure?project=nlog.web&branch=master&metric=ncloc)](https://sonarcloud.io/dashboard/?id=nlog.web&branch=master)
Expand Down
9 changes: 8 additions & 1 deletion src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,15 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv

private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogAspNetCoreOptions options, NLog.LogFactory logFactory)
{
configuration = SetupConfiguration(serviceProvider, configuration);
NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogAspNetCoreOptions.Default, logFactory ?? LogManager.LogFactory);

configuration = SetupConfiguration(serviceProvider, configuration);

if (serviceProvider != null && provider.Options.RegisterServiceProvider)
{
provider.LogFactory.ServiceRepository.RegisterService(typeof(IServiceProvider), serviceProvider);
}

if (configuration != null)
{
provider.Configure(configuration.GetSection("Logging:NLog"));
Expand Down
4 changes: 2 additions & 2 deletions tests/Shared/LayoutRenderers/W3CLoggerLayoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void W3CLoggerLayoutNoContextTest()
[Fact]
public void W3CLoggerLayoutWithContextTest()
{
var httpContextMock = SetupHttpAccessorWithHttpContext("www.google.com:80", "http", "/Test.asp", "?t=1");
var httpContextMock = SetupHttpAccessorWithHttpContext("nlog-project.org:80", "http", "/Test.asp", "?t=1");

var logFactory = new NLog.LogFactory().Setup().SetupExtensions(ext => ext.RegisterAssembly(typeof(NLog.Web.Layouts.W3CExtendedLogLayout).Assembly)).LoadConfiguration(builder =>
{
Expand All @@ -69,7 +69,7 @@ public void W3CLoggerLayoutWithContextTest()
var logEvent = new LogEventInfo(LogLevel.Info, null, "RequestLogging");
logger.Log(logEvent);
string expectedFieldHeaders = "c-ip cs-username s-computername cs-method cs-uri-stem cs-uri-query sc-status sc-bytes cs-bytes time-taken cs-host cs(User-Agent)";
string expectedFieldValues = $"- - {Environment.MachineName} - /Test.asp ?t=1 200 7 42 - www.google.com -";
string expectedFieldValues = $"- - {Environment.MachineName} - /Test.asp ?t=1 200 7 42 - nlog-project.org -";
string expectedHeader = $@"#Software: Microsoft Internet Information Server{System.Environment.NewLine}#Version: 1.0{System.Environment.NewLine}#Start-Date: {logEvent.TimeStamp.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)}{System.Environment.NewLine}#Fields: date time {expectedFieldHeaders}";
string expectedBody = $@"{logEvent.TimeStamp.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)} {expectedFieldValues}";

Expand Down

0 comments on commit 08b4eeb

Please sign in to comment.