Skip to content
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
72 changes: 72 additions & 0 deletions NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#if NETCOREAPP2_0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Castle.Core.Logging;
using Xunit;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Config;
using NLog.Targets;
using NLog.Web.Tests.LayoutRenderers;


namespace NLog.Web.AspNetCore.Tests
{
public class AspNetCoreTests : TestBase, IDisposable
{
#region IDisposable

/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
LogManager.Configuration = null;
}

#endregion

[Fact]
public void UseNLogShouldLogTest()
{
var webhost =
Microsoft.AspNetCore.WebHost.CreateDefaultBuilder()
.Configure(c => c.New()) //.New needed, otherwise:
// Unhandled Exception: System.ArgumentException: A valid non-empty application name must be provided.
// Parameter name: applicationName
.UseNLog() //use NLog for ILoggers and pass httpcontext
.Build();


var loggerFact = webhost.Services.GetService<Microsoft.Extensions.Logging.ILoggerFactory>();

Assert.NotNull(loggerFact);

var configuration = new LoggingConfiguration();
var target = new MemoryTarget("target1");
target.Layout = "${logger}|${message}";

configuration.AddRuleForAllLevels(target);

LogManager.Configuration = configuration;

var logger = loggerFact.CreateLogger("logger1");

logger.LogError("error1");

var logged = target.Logs;

Assert.Single(logged);
Assert.Equal("logger1|error1", logged.First());


}

}
}

#endif
20 changes: 12 additions & 8 deletions NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net452;netcoreapp1.0;netcoreapp2.0;</TargetFrameworks>
<TargetFrameworks>netcoreapp1.1;netcoreapp2.0;net461</TargetFrameworks>
<DefineConstants>$(DefineConstants);ASP_NET_CORE</DefineConstants>
<AssemblyName>NLog.Web.AspNetCore.Tests</AssemblyName>
<AssemblyVersion>1.2.3.0</AssemblyVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NLog.Web.AspNetCore\NLog.Web.AspNetCore.csproj" />
<PackageReference Include="NSubstitute" Version="2.0.3" />
<PackageReference Include="Castle.Core" Version="4.2.0" />
<!-- Castle.Core upgrade for NSubstitute-->

<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta5-*" />
<PackageReference Include="xunit" Version="2.3.0-beta5-*" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta5-*" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta5-build3769" />
<PackageReference Include="xunit" Version="2.3.0-beta5-build3769" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<!--
icrosoft.NET.Test.Sdk needed for xunit.runner.visualstudio
-->
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta5-build3769" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'netcoreapp2.0' ">
<PackageReference Include="Microsoft.AspNetCore.Http" Version="1.1.1" />
Expand All @@ -20,10 +27,7 @@
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="1.1.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
Expand Down
43 changes: 42 additions & 1 deletion NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

using Microsoft.Extensions.Logging;
using NLog.Config;
using NLog.Web.Internal;
using NLog.Extensions.Logging;


#if NETSTANDARD2_0
using Microsoft.Extensions.DependencyInjection;
#endif

namespace NLog.Web
{
/// <summary>
Expand All @@ -22,7 +27,9 @@ public static class AspNetExtensions
/// Enable NLog Web for ASP.NET Core.
/// </summary>
/// <param name="app"></param>

#if NETSTANDARD2_0
[Obsolete("Use UseNLog() on IWebHostBuilder")]
#endif
public static void AddNLogWeb(this IApplicationBuilder app)
{
ServiceLocator.ServiceProvider = app.ApplicationServices;
Expand Down Expand Up @@ -53,5 +60,39 @@ private static LoggingConfiguration ConfigureNLog(string fileName)
}


#if NETSTANDARD2_0


/// <summary>
/// Use NLog for Dependency Injected loggers.
/// </summary>
public static IWebHostBuilder UseNLog(this IWebHostBuilder builder)
{
return UseNLog(builder, null);
}

/// <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 IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogProviderOptions options)
{
if (builder == null) throw new ArgumentNullException(nameof(builder));
builder.ConfigureServices(services =>
services.AddSingleton<ILoggerFactory>(serviceProvider =>
{
ServiceLocator.ServiceProvider = serviceProvider;
ServiceLocator.Services = services;

return new NLogLoggerFactory();
})
);
return builder;
}
#endif


}
}
12 changes: 10 additions & 2 deletions NLog.Web.AspNetCore/Internal/ServiceLocator.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;

namespace NLog.Web.Internal
{
/// <summary>
/// Service provider
/// </summary>
/// <remarks>
/// This is a anti-pattern, but it works well with NLog
/// </remarks>
internal static class ServiceLocator
{
/// <summary>
/// The current service provider for reading ASP.NET 5 session, request etc.
/// The current service provider for reading ASP.NET Core session, request etc.
/// </summary>
/// <remarks>This is a anti-pattern, but for now there is not other solution to fix this.</remarks>
public static IServiceProvider ServiceProvider { get; set; }

/// <summary>
/// Registering needed services
/// </summary>
public static IServiceCollection Services { get; set; }
}
}
2 changes: 1 addition & 1 deletion NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@



<PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-beta6" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-beta7" />
<PackageReference Include="System.ValueTuple" Version="4.3.0" />
</ItemGroup>

Expand Down