Skip to content

Commit

Permalink
Merge pull request #249 from snakefoot/master
Browse files Browse the repository at this point in the history
Automatically register NLog.Web LayoutRenders for NetCoreApp1
  • Loading branch information
304NotModified authored Feb 21, 2018
2 parents 8e4072a + d1ed6b5 commit b9caa94
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
38 changes: 32 additions & 6 deletions NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NETCOREAPP2_0
#if ASP_NET_CORE

using System;
using System.Collections.Generic;
Expand All @@ -12,13 +12,13 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using NLog.Config;
using NLog.Layouts;
using NLog.Targets;
using NLog.Web.Tests.LayoutRenderers;
using ILoggerFactory = Microsoft.Extensions.Logging.ILoggerFactory;


namespace NLog.Web.AspNetCore.Tests
{
public class AspNetCoreTests : TestBase, IDisposable
Expand Down Expand Up @@ -104,29 +104,29 @@ public void UseAspNetWithoutRegister()
}




[Fact]
public void RegisterHttpContext()
{
var webhost = CreateWebHost();
Assert.NotNull(webhost.Services.GetService<IHttpContextAccessor>());

}

#if ASP_NET_CORE2
[Fact]
public void SkipRegisterHttpContext()
{
var webhost = CreateWebHost(new NLogAspNetCoreOptions { RegisterHttpContextAccessor = false });
Assert.Null(webhost.Services.GetService<IHttpContextAccessor>());

}
#endif

/// <summary>
/// Create webhost with UseNlog
/// </summary>
/// <returns></returns>
private static IWebHost CreateWebHost(NLogAspNetCoreOptions options = null)
{
#if ASP_NET_CORE2
var webhost =
Microsoft.AspNetCore.WebHost.CreateDefaultBuilder()
.Configure(c => c.New()) //.New needed, otherwise:
Expand All @@ -135,13 +135,39 @@ private static IWebHost CreateWebHost(NLogAspNetCoreOptions options = null)
.UseNLog(options) //use NLog for ILoggers and pass httpcontext
.Build();
return webhost;
#else
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.Build();
return host;
#endif
}

private static ILoggerFactory GetLoggerFactory(IWebHost webhost)
{
return webhost.Services.GetService<Microsoft.Extensions.Logging.ILoggerFactory>();
}

#if !ASP_NET_CORE2
public class Startup
{
public Startup()
{
}

public void Configure(Microsoft.AspNetCore.Builder.IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.AddNLogWeb();
loggerFactory.AddNLog();
}

public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
}
#endif
}
}

Expand Down
5 changes: 1 addition & 4 deletions NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
<PackageReference Include="Microsoft.AspNetCore.Http" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'netcoreapp1.1' ">
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.1" />
Expand Down
4 changes: 4 additions & 0 deletions NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public static class AspNetExtensions
public static void AddNLogWeb(this IApplicationBuilder app)
{
ServiceLocator.ServiceProvider = app.ApplicationServices;
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
}

/// <summary>
Expand All @@ -43,6 +45,8 @@ public static void AddNLogWeb(this IApplicationBuilder app)
/// <returns>LoggingConfiguration for chaining</returns>
public static LoggingConfiguration ConfigureNLog(this IHostingEnvironment env, string configFileRelativePath)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
var fileName = Path.Combine(env.ContentRootPath, configFileRelativePath);
LogManager.LoadConfiguration(fileName);
return LogManager.Configuration;
Expand Down

0 comments on commit b9caa94

Please sign in to comment.