Skip to content

Commit

Permalink
Merge pull request #335 from snakefoot/master
Browse files Browse the repository at this point in the history
Register WebHost configuration with ${configsetting} and LoggingProvider-options
  • Loading branch information
304NotModified authored Jan 25, 2019
2 parents dcdf815 + a34761f commit 6335740
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
2 changes: 1 addition & 1 deletion NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'netcoreapp1.1' ">
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.0" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
Expand Down
67 changes: 52 additions & 15 deletions NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Microsoft.Extensions.Configuration.IConfiguration>());
});
return builder;
}

//note: when registering ILoggerFactory, all non NLog stuff and stuff before this will be removed
services.AddSingleton<ILoggerProvider>(serviceProvider =>
{
ServiceLocator.ServiceProvider = serviceProvider;
return new NLogLoggerProvider(options);
});
/// <summary>
/// Use NLog for Dependency Injected loggers.
/// </summary>
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<ILoggerFactory>
if (options.RegisterHttpContextAccessor)
/// <summary>
/// Use NLog for Dependency Injected loggers.
/// </summary>
/// <param name="builder"></param>
/// <param name="options">Options for logging to NLog with Dependency Injected loggers</param>
/// <returns></returns>
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<IServiceProvider, Microsoft.Extensions.Configuration.IConfiguration> lookupConfiguration)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);

services.AddSingleton<ILoggerProvider>(serviceProvider =>
{
ServiceLocator.ServiceProvider = serviceProvider;

var provider = new NLogLoggerProvider(options ?? new NLogProviderOptions());
var configuration = lookupConfiguration(serviceProvider);
if (configuration != null)
{
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
if (options == null)
provider.Configure(configuration.GetSection("Logging:NLog"));
}
return provider;
});
return builder;

//note: this one is called before services.AddSingleton<ILoggerFactory>
if ((options ?? NLogAspNetCoreOptions.Default).RegisterHttpContextAccessor)
{
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
}
#endif

Expand Down
18 changes: 9 additions & 9 deletions NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<TargetFrameworks>netstandard1.5;net451;net461;netstandard2.0</TargetFrameworks>

<VersionPrefix>4.7.1</VersionPrefix>
<VersionPrefix>4.8</VersionPrefix>
<VersionSuffix></VersionSuffix>
<Version>$(VersionPrefix)$(VersionSuffix)</Version>
<InformationalVersion>$(Version)</InformationalVersion>
Expand All @@ -30,10 +30,10 @@ Supported platforms:
<PackageId>NLog.Web.AspNetCore</PackageId>
<PackageTags>logging;log;session;NLog;web;aspnet;aspnetcore;MVC;httpcontext</PackageTags>
<PackageReleaseNotes>
- 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
</PackageReleaseNotes>
<PackageIconUrl>http://nlog-project.org/N.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/NLog/NLog.Web</PackageProjectUrl>
Expand Down Expand Up @@ -74,7 +74,7 @@ Supported platforms:
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NLog.Extensions.Logging" Version="1.3.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.4.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
Expand All @@ -89,9 +89,9 @@ Supported platforms:
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net461' ">
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="2.1.0" />
</ItemGroup>

<PropertyGroup>
Expand Down

0 comments on commit 6335740

Please sign in to comment.