Skip to content

Commit

Permalink
TargetFramework netcoreapp3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Oct 7, 2019
1 parent 0fed91b commit 04d6710
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 56 deletions.
14 changes: 10 additions & 4 deletions src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using NLog.Config;
using NLog.Extensions.Logging;
using NLog.Web.DependencyInjection;
#if ASP_NET_CORE1 || ASP_NET_CORE2
using Microsoft.AspNetCore.Builder;
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
#if ASP_NET_CORE2
#endif
#if ASP_NET_CORE2 || ASP_NET_CORE3
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -23,6 +25,7 @@ namespace NLog.Web
/// </summary>
public static class AspNetExtensions
{
#if ASP_NET_CORE1 || ASP_NET_CORE2
/// <summary>
/// Enable NLog Web for ASP.NET Core.
/// </summary>
Expand All @@ -34,7 +37,9 @@ public static void AddNLogWeb(this IApplicationBuilder app)
{
app.ApplicationServices.SetupNLogServiceLocator();
}
#endif

#if ASP_NET_CORE1 || ASP_NET_CORE2
/// <summary>
/// Apply NLog configuration from XML config.
/// </summary>
Expand All @@ -52,6 +57,7 @@ public static LoggingConfiguration ConfigureNLog(this IHostingEnvironment env, s
LogManager.LoadConfiguration(fileName);
return LogManager.Configuration;
}
#endif

/// <summary>
/// Override the default <see cref="IServiceProvider" /> used by the NLog ServiceLocator.
Expand All @@ -69,7 +75,7 @@ public static IServiceProvider SetupNLogServiceLocator(this IServiceProvider ser
return serviceProvider;
}

#if ASP_NET_CORE2
#if ASP_NET_CORE2 || ASP_NET_CORE3

/// <summary>
/// Apply NLog configuration from XML config.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using System;
using System.Text;
#if ASP_NET_CORE1 || ASP_NET_CORE2
using Microsoft.AspNetCore.Hosting;
using IHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
#endif
#if ASP_NET_CORE3
using Microsoft.Extensions.Hosting;
#endif
using Microsoft.Extensions.DependencyInjection;
using NLog.Config;
using NLog.LayoutRenderers;
Expand All @@ -16,9 +22,9 @@ namespace NLog.Web.LayoutRenderers
[ThreadSafe]
public class AspNetEnvironmentLayoutRenderer : LayoutRenderer
{
private static IHostingEnvironment _hostingEnvironment;
private static IHostEnvironment _hostEnvironment;

private static IHostingEnvironment HostingEnvironment => _hostingEnvironment ?? (_hostingEnvironment = ServiceLocator.ServiceProvider?.GetService<IHostingEnvironment>());
private static IHostEnvironment HostEnvironment => _hostEnvironment ?? (_hostEnvironment = ServiceLocator.ServiceProvider?.GetService<IHostEnvironment>());

/// <summary>
/// Append to target
Expand All @@ -27,13 +33,13 @@ public class AspNetEnvironmentLayoutRenderer : LayoutRenderer
/// <param name="logEvent">Logging event.</param>
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
builder.Append(HostingEnvironment?.EnvironmentName);
builder.Append(HostEnvironment?.EnvironmentName);
}

/// <inheritdoc />
protected override void CloseLayoutRenderer()
{
_hostingEnvironment = null;
_hostEnvironment = null;
base.CloseLayoutRenderer();
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<TargetFrameworks>netstandard1.5;net451;net461;netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard1.5;net451;net461;netstandard2.0;netcoreapp3.0</TargetFrameworks>
<Product>NLog.Web.AspNetCore v$(VersionPrefix)</Product>
<Description>
NLog LoggerProvider for Microsoft.Extensions.Logging and ASP.NET Core platform. Adds helpers and layout renderers for websites and web applications.

Supported platforms:

- For ASP.NET Core 3, .NET Core 3.0
- For ASP.NET Core 2, .NET Standard 2.0+ and .NET 4.6+
- For ASP.NET Core 1, .NET Standard 1.5+ and .NET 4.5.x
</Description>
Expand Down Expand Up @@ -55,14 +56,18 @@ Supported platforms:
<Title>ASP.NET Core 2 integration for NLog - .NET Standard 2</Title>
<DefineConstants>$(DefineConstants);ASP_NET_CORE;ASP_NET_CORE2</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.0' ">
<Title>ASP.NET Core 3 integration for NLog - .NET Core 3</Title>
<DefineConstants>$(DefineConstants);ASP_NET_CORE;ASP_NET_CORE3</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog.Extensions.Logging" Version="1.5.4" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'net461' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.5' or '$(TargetFramework)' == 'net451' ">
<PackageReference Include="Microsoft.AspNetCore.Http" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="1.1.2" />
Expand All @@ -73,6 +78,9 @@ Supported platforms:
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="2.1.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.0' ">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Shared\**\*.cs" />
</ItemGroup>
Expand Down
17 changes: 11 additions & 6 deletions src/Shared/LayoutRenderers/AspNetAppBasePathLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
using NLog.Config;
using NLog.LayoutRenderers;
#if ASP_NET_CORE
#if ASP_NET_CORE1 || ASP_NET_CORE2
using Microsoft.AspNetCore.Hosting;
using IHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
#endif
#if ASP_NET_CORE3
using Microsoft.Extensions.Hosting;
#endif
using Microsoft.Extensions.DependencyInjection;
using NLog.Web.DependencyInjection;

#else
using System.Web.Hosting;
#endif
Expand All @@ -16,7 +21,7 @@ namespace NLog.Web.LayoutRenderers
{
#if ASP_NET_CORE
/// <summary>
/// Rendering Application BasePath. <see cref="IHostingEnvironment.ContentRootPath" /> (Previous IApplicationEnvironment.ApplicationBasePath)
/// Rendering Application BasePath. <see cref="IHostEnvironment.ContentRootPath" /> (Previous IApplicationEnvironment.ApplicationBasePath)
/// </summary>
#else
/// <summary>
Expand All @@ -29,11 +34,11 @@ namespace NLog.Web.LayoutRenderers
public class AspNetAppBasePathLayoutRenderer : LayoutRenderer
{
#if ASP_NET_CORE
private static IHostingEnvironment _hostingEnvironment;
private static IHostEnvironment _hostEnvironment;

private static IHostingEnvironment HostingEnvironment => _hostingEnvironment ?? (_hostingEnvironment = ServiceLocator.ServiceProvider?.GetService<IHostingEnvironment>());
private static IHostEnvironment HostEnvironment => _hostEnvironment ?? (_hostEnvironment = ServiceLocator.ServiceProvider?.GetService<IHostEnvironment>());

private string AppBasePath => HostingEnvironment?.ContentRootPath ?? Directory.GetCurrentDirectory();
private string AppBasePath => HostEnvironment?.ContentRootPath ?? Directory.GetCurrentDirectory();
#else
private string AppBasePath => _appBasePath ?? (_appBasePath = HostingEnvironment.MapPath("~"));
private static string _appBasePath;
Expand All @@ -53,7 +58,7 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
protected override void CloseLayoutRenderer()
{
#if ASP_NET_CORE
_hostingEnvironment = null;
_hostEnvironment = null;
#else
_appBasePath = null;
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
using NLog.LayoutRenderers;
#if ASP_NET_CORE
using Microsoft.AspNetCore.Http;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
using Microsoft.Extensions.DependencyInjection;
using NLog.Web.DependencyInjection;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
#else
using System.Web;
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/LayoutRenderers/AspNetRequestPostedBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private bool TryEnableBuffering(HttpRequest httpRequest, long? contentLength, ou

private static Stream EnableRewind(HttpRequest httpRequest, int bufferThreshold)
{
#if ASP_NET_CORE2
#if ASP_NET_CORE2 || ASP_NET_CORE3
Microsoft.AspNetCore.Http.HttpRequestRewindExtensions.EnableBuffering(httpRequest, bufferThreshold);
return httpRequest.Body;
#elif ASP_NET_CORE1
Expand Down
12 changes: 7 additions & 5 deletions src/Shared/LayoutRenderers/AspNetWebRootPathLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using NLog.LayoutRenderers;
#if ASP_NET_CORE
using Microsoft.AspNetCore.Hosting;
#if ASP_NET_CORE1 || ASP_NET_CORE2
using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
#endif
using Microsoft.Extensions.DependencyInjection;
using NLog.Web.DependencyInjection;

#else
using System.Web.Hosting;
#endif
Expand All @@ -28,11 +30,11 @@ namespace NLog.Web.LayoutRenderers
public class AspNetWebRootPathLayoutRenderer : LayoutRenderer
{
#if ASP_NET_CORE
private static IHostingEnvironment _hostingEnvironment;
private static IWebHostEnvironment _webHostEnvironment;

private static IHostingEnvironment HostingEnvironment => _hostingEnvironment ?? (_hostingEnvironment = ServiceLocator.ServiceProvider?.GetService<IHostingEnvironment>());
private static IWebHostEnvironment WebHostEnvironment => _webHostEnvironment ?? (_webHostEnvironment = ServiceLocator.ServiceProvider?.GetService<IWebHostEnvironment>());

private string WebRootPath => HostingEnvironment?.WebRootPath;
private string WebRootPath => WebHostEnvironment?.WebRootPath;
#else
private string WebRootPath => _webRootPath ?? (_webRootPath = HostingEnvironment.MapPath("/"));
private static string _webRootPath;
Expand All @@ -52,7 +54,7 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
protected override void CloseLayoutRenderer()
{
#if ASP_NET_CORE
_hostingEnvironment = null;
_webHostEnvironment = null;
#else
_webRootPath = null;
#endif
Expand Down
17 changes: 11 additions & 6 deletions src/Shared/LayoutRenderers/IISInstanceNameLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
#if !ASP_NET_CORE
using System.Web.Hosting;
#else
using NLog.Web.DependencyInjection;
#if ASP_NET_CORE1 || ASP_NET_CORE2
using Microsoft.AspNetCore.Hosting;
using IHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
#endif
#if ASP_NET_CORE3
using Microsoft.Extensions.Hosting;
#endif
using Microsoft.Extensions.DependencyInjection;

using NLog.Web.DependencyInjection;
#endif

namespace NLog.Web.LayoutRenderers
Expand All @@ -29,9 +34,9 @@ namespace NLog.Web.LayoutRenderers
public class IISInstanceNameLayoutRenderer : LayoutRenderer
{
#if ASP_NET_CORE
private static IHostingEnvironment _hostingEnvironment;
private static IHostEnvironment _hostEnvironment;

private static IHostingEnvironment HostingEnvironment => _hostingEnvironment ?? (_hostingEnvironment = ServiceLocator.ServiceProvider?.GetService<IHostingEnvironment>());
private static IHostEnvironment HostEnvironment => _hostEnvironment ?? (_hostEnvironment = ServiceLocator.ServiceProvider?.GetService<IHostEnvironment>());
#endif

/// <summary>
Expand All @@ -42,7 +47,7 @@ public class IISInstanceNameLayoutRenderer : LayoutRenderer
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
#if ASP_NET_CORE
builder.Append(HostingEnvironment?.ApplicationName);
builder.Append(HostEnvironment?.ApplicationName);
#else
builder.Append(HostingEnvironment.SiteName);
#endif
Expand All @@ -52,7 +57,7 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
/// <inheritdoc />
protected override void CloseLayoutRenderer()
{
_hostingEnvironment = null;
_hostEnvironment = null;
base.CloseLayoutRenderer();
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void SkipRegisterHttpContext()
/// <returns></returns>
private static IWebHost CreateWebHost(NLogAspNetCoreOptions options = null)
{
#if ASP_NET_CORE2
#if ASP_NET_CORE2 || ASP_NET_CORE3
var webhost =
Microsoft.AspNetCore.WebHost.CreateDefaultBuilder()
.Configure(c => c.New()) //.New needed, otherwise:
Expand All @@ -149,7 +149,7 @@ private static ILoggerFactory GetLoggerFactory(IWebHost webhost)
return webhost.Services.GetService<Microsoft.Extensions.Logging.ILoggerFactory>();
}

#if !ASP_NET_CORE2
#if !ASP_NET_CORE2 && !ASP_NET_CORE3
public class Startup
{
public Startup()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;netcoreapp2;net461</TargetFrameworks>
<TargetFrameworks>netcoreapp1.1;netcoreapp2.1;net461;netcoreapp3.0</TargetFrameworks>
<AssemblyName>NLog.Web.AspNetCore.Tests</AssemblyName>
<AssemblyVersion>1.2.3.0</AssemblyVersion>
<FileVersion>1.2.3.1</FileVersion>
Expand All @@ -14,10 +14,13 @@
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
<DefineConstants>$(DefineConstants);ASP_NET_CORE;ASP_NET_CORE1</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' != 'netcoreapp1.1' ">
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1' or '$(TargetFramework)' == 'net461' ">
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
<DefineConstants>$(DefineConstants);ASP_NET_CORE;ASP_NET_CORE2</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.0' ">
<DefineConstants>$(DefineConstants);ASP_NET_CORE;ASP_NET_CORE3</DefineConstants>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\NLog.Web.AspNetCore\NLog.Web.AspNetCore.csproj" />
<PackageReference Include="NSubstitute" Version="4.2.1" />
Expand All @@ -29,7 +32,7 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'netcoreapp1.1' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1' or '$(TargetFramework)' == 'net461' ">
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.0" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using HttpSessionState = Microsoft.AspNetCore.Http.ISession;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Http.Internal;
#endif
using NLog.Web.LayoutRenderers;
using NSubstitute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ public class AspNetRequestHeadersLayoutRendererTests : TestInvolvingAspNetHttpCo
[Fact]
public void NullKeyRendersAllHeaders()
{
#if ASP_NET_CORE
var expectedResult = "Host=stackoverflow.com,key=TEST,Key1=TEST1"; // ASP.NET Core automatically includes host of request URL as part of headers
#else
var expectedResult = "key=TEST,Key1=TEST1";
#endif
var renderer = CreateRenderer();
renderer.HeaderNames = null;

Expand All @@ -35,11 +31,7 @@ public void NullKeyRendersAllHeaders()
[Fact]
public void NullKeyRendersAllHeadersExceptExcluded()
{
#if ASP_NET_CORE
var expectedResult = "Host=stackoverflow.com,Key1=TEST1"; // ASP.NET Core automatically includes host of request URL as part of headers
#else
var expectedResult = "Key1=TEST1";
#endif
var renderer = CreateRenderer();
renderer.HeaderNames = null;
renderer.Exclude.Add("key");
Expand Down
Loading

0 comments on commit 04d6710

Please sign in to comment.