Skip to content

Commit

Permalink
Merge pull request #550 from NLog/release/4.9.2
Browse files Browse the repository at this point in the history
Release 4.9.2
  • Loading branch information
304NotModified authored Apr 20, 2020
2 parents 3e3c055 + 0bc6945 commit d01b128
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 25 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ See also [releases](https://github.com/NLog/NLog.Web/releases) and [milestones](

Date format: (year/month/day)

### v4.9.2-aspnetcore (2020/04/20)
- [#546](https://github.com/NLog/NLog.Web/pull/546) ${aspnet-request-ip}: make ForwardedForHeader configurable (@304NotModified)
- [#546](https://github.com/NLog/NLog.Web/pull/546) ${aspnet-request-url} Added UseRawTarget option (@sm-g, @304NotModified)

### v4.9.2-aspnet4 (2020/04/20)
- [#546](https://github.com/NLog/NLog.Web/pull/546) ${aspnet-request-ip}: make ForwardedForHeader configurable (@304NotModified)

### v4.9.1-aspnetcore (2020/03/25)
- [#524](https://github.com/NLog/NLog.Web/pull/524) ${aspnet-traceidentifier} should automatically check Activity.Current.Id for NetCore3 (@snakefoot)
- [#524](https://github.com/NLog/NLog.Web/pull/524) ${aspnet-traceidentifier} should automatically check Activity.Current.Id for .NET Core 3 (@snakefoot)
- [#527](https://github.com/NLog/NLog.Web/pull/527) Improved AddNLog with LoggingConfiguration to handle custom LogFactory (@snakefoot)

### v4.9.1-aspnet4 (2020/03/25)
- Updated dependencies

### v4.9.0-aspnetcore (2019/10/11)
This version adds support for ASP.NET Core 3 and some nice renderers has been added and improved!

Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 4.9.1.{build}
version: 4.9.2.{build}
clone_folder: c:\projects\nlogweb
configuration: Release
image: Visual Studio 2019
Expand All @@ -22,7 +22,7 @@ skip_tags: true

build_script:
- ps: |
$versionPrefix = "4.9.1"
$versionPrefix = "4.9.2"
$versionSuffix = ""
$versionBuild = $versionPrefix + "." + ${env:APPVEYOR_BUILD_NUMBER}
$versionNuget = $versionPrefix
Expand Down
6 changes: 4 additions & 2 deletions src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ Supported platforms:
<PackageId>NLog.Web.AspNetCore</PackageId>
<PackageTags>logging;log;NLog;web;aspnet;aspnetcore;MVC;Microsoft.Extensions.Logging;httpcontext;session</PackageTags>
<PackageReleaseNotes>
- ${aspnet-traceidentifier} should automatically check Activity.Current.Id for NetCore3 (@snakefoot)
- Improved AddNLog with LoggingConfiguration to handle custom LogFactory (@snakefoot)
- ${aspnet-request-ip}: make ForwardedForHeader configurable (@304NotModified)
- ${aspnet-request-url} Added UseRawTarget option (@sm-g, @304NotModified)

See also https://github.com/NLog/NLog.Web/releases
</PackageReleaseNotes>
<PackageIconUrl>https://nlog-project.org/N.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/NLog/NLog.Web</PackageProjectUrl>
Expand Down
4 changes: 3 additions & 1 deletion src/NLog.Web/NLog.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ For ASP.NET Core: Check https://www.nuget.org/packages/NLog.Web.AspNetCore
<PackageTags>nlog;log;logging;target;layoutrenderer;web;asp.net;MVC;httpcontext</PackageTags>
<PackageReleaseNotes>

See https://github.com/NLog/NLog.Web/releases

</PackageReleaseNotes>
<PackageIconUrl>https://nlog-project.org/N.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/NLog/NLog.Web</PackageProjectUrl>
Expand All @@ -36,7 +38,7 @@ For ASP.NET Core: Check https://www.nuget.org/packages/NLog.Web.AspNetCore
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="4.6.8" />
<PackageReference Include="NLog" Version="4.7.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Web" />
Expand Down
28 changes: 19 additions & 9 deletions src/Shared/LayoutRenderers/AspNetRequestIpLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System;
using System.ComponentModel;
using System.Text;

using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Layouts;
using NLog.Web.Internal;
#if ASP_NET_CORE
using Microsoft.AspNetCore.Http;

#else
using System.Web;
#endif

namespace NLog.Web.LayoutRenderers
Expand All @@ -22,10 +26,14 @@ namespace NLog.Web.LayoutRenderers
[ThreadSafe]
public class AspNetRequestIpLayoutRenderer : AspNetLayoutRendererBase
{
private const string ForwardedForHeader = "X-Forwarded-For";
/// <summary>
/// The header name to check for the Forwarded-For. Default "X-Forwarded-For". Needs <see cref="CheckForwardedForHeader"/>
/// </summary>
[DefaultValue("X-Forwarded-For")]
public Layout ForwardedForHeader { get; set; } = "X-Forwarded-For";

/// <summary>
/// Gets or sets whether the renderer should check value of X-Forwarded-For header
/// Gets or sets whether the renderer should check value of <see cref="ForwardedForHeader"/> header
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public bool CheckForwardedForHeader { get; set; }
Expand All @@ -43,7 +51,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
return;
}

var ip = CheckForwardedForHeader ? TryLookupForwardHeader(request) : string.Empty;
var ip = CheckForwardedForHeader && ForwardedForHeader != null ? TryLookupForwardHeader(request, logEvent) : string.Empty;

if (string.IsNullOrEmpty(ip))
{
Expand All @@ -58,9 +66,10 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}

#if !ASP_NET_CORE
string TryLookupForwardHeader(System.Web.HttpRequestBase httpRequest)
string TryLookupForwardHeader(HttpRequestBase httpRequest, LogEventInfo logEvent)
{
var forwardedHeader = httpRequest.Headers[ForwardedForHeader];
var headerName = ForwardedForHeader.Render(logEvent);
var forwardedHeader = httpRequest.Headers[headerName];

if (!string.IsNullOrEmpty(forwardedHeader))
{
Expand All @@ -74,11 +83,12 @@ string TryLookupForwardHeader(System.Web.HttpRequestBase httpRequest)
return string.Empty;
}
#else
private string TryLookupForwardHeader(HttpRequest httpRequest)
private string TryLookupForwardHeader(HttpRequest httpRequest, LogEventInfo logEvent)
{
if (httpRequest.Headers?.ContainsKey(ForwardedForHeader) == true)
var headerName = ForwardedForHeader.Render(logEvent);
if (httpRequest.Headers?.ContainsKey(headerName) == true)
{
var forwardedHeaders = httpRequest.Headers.GetCommaSeparatedValues(ForwardedForHeader);
var forwardedHeaders = httpRequest.Headers.GetCommaSeparatedValues(headerName);
if (forwardedHeaders.Length > 0)
{
return forwardedHeaders[0];
Expand Down
35 changes: 28 additions & 7 deletions src/Shared/LayoutRenderers/AspNetRequestUrlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NLog.Web.Internal;
#if ASP_NET_CORE
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;

#else
using System.Collections.Specialized;
Expand All @@ -24,7 +25,7 @@ namespace NLog.Web.LayoutRenderers
/// ${aspnet-request-url:IncludePort=true} - produces http://www.exmaple.com:80/
/// ${aspnet-request-url:IncludePort=false} - produces http://www.exmaple.com/
/// ${aspnet-request-url:IncludeScheme=false} - produces www.exmaple.com/
/// ${aspnet-request-url:IncludePort=true:IncludeQueryString=true} - produces http://www.exmaple.com:80/?t=1
/// ${aspnet-request-url:IncludePort=true:IncludeQueryString=true} - produces http://www.exmaple.com:80/?t=1
/// </code>
/// </example>
[LayoutRenderer("aspnet-request-url")]
Expand All @@ -37,7 +38,7 @@ public class AspNetRequestUrlRenderer : AspNetLayoutRendererBase
public bool IncludeQueryString { get; set; } = false;

/// <summary>
/// To specify whether to include /exclude the Port. Default is false.
/// To specify whether to include / exclude the Port. Default is false.
/// </summary>
public bool IncludePort { get; set; } = false;

Expand All @@ -51,6 +52,16 @@ public class AspNetRequestUrlRenderer : AspNetLayoutRendererBase
/// </summary>
public bool IncludeScheme { get; set; } = true;

#if ASP_NET_CORE

/// <summary>
/// To specify whether to use raw path and full query. Default is false.
/// See https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.features.ihttprequestfeature.rawtarget
/// </summary>
public bool UseRawTarget { get; set; } = false;

#endif

/// <summary>
/// Renders the Request URL from the HttpRequest
/// </summary>
Expand All @@ -68,6 +79,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}

#if !ASP_NET_CORE

private void RenderUrl(HttpRequestBase httpRequest, StringBuilder builder)
{
var url = httpRequest.Url;
Expand Down Expand Up @@ -112,13 +124,22 @@ private void RenderUrl(HttpRequest httpRequest, StringBuilder builder)
builder.Append(httpRequest.Host.Port.Value);
}

builder.Append(httpRequest.PathBase.ToUriComponent());
builder.Append(httpRequest.Path.ToUriComponent());
if (IncludeQueryString)
IHttpRequestFeature httpRequestFeature;
if (UseRawTarget && (httpRequestFeature = httpRequest.HttpContext.Features.Get<IHttpRequestFeature>()) != null)
{
builder.Append(httpRequestFeature.RawTarget);
}
else
{
builder.Append(httpRequest.QueryString.Value);
builder.Append(httpRequest.PathBase.ToUriComponent());
builder.Append(httpRequest.Path.ToUriComponent());

if (IncludeQueryString)
{
builder.Append(httpRequest.QueryString.Value);
}
}
}
#endif
}
}
}
4 changes: 2 additions & 2 deletions tests/NLog.Web.Tests/NLog.Web.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<PackageReference Include="System.IO.Compression" Version="4.*" />
<PackageReference Include="System.Net.Http" Version="4.*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
<PackageReference Include="NSubstitute" Version="4.2.1" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
25 changes: 25 additions & 0 deletions tests/Shared/LayoutRenderers/AspNetRequestIpLayoutRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,31 @@ public void ForwardedForHeaderContainsMultipleEntriesRenderFirstValue()
// Act
string result = renderer.Render(new LogEventInfo());

// Assert
Assert.Equal("127.0.0.1", result);
}

[Fact]
public void ForwardedForHeaderPresentWithCustomRenderForwardedValue()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();

#if !ASP_NET_CORE
httpContext.Request.ServerVariables.Returns(new NameValueCollection {{"REMOTE_ADDR", "192.0.0.0"}});
httpContext.Request.Headers.Returns(
new NameValueCollection {{"header2", "127.0.0.1"}});
#else
var headers = new HeaderDict();
headers.Add("header2", new StringValues("127.0.0.1"));
httpContext.Request.Headers.Returns(callinfo => headers);
#endif
renderer.CheckForwardedForHeader = true;
renderer.ForwardedForHeader = "header2";

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
Assert.Equal("127.0.0.1", result);
}
Expand Down
62 changes: 61 additions & 1 deletion tests/Shared/LayoutRenderers/AspNetRequestUrlRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
using Microsoft.Extensions.Primitives;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
#endif
using NLog.Web.LayoutRenderers;
using NSubstitute;

using Xunit;

using System.IO;

namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetRequestUrlRendererTests : LayoutRenderersTestBase<AspNetRequestUrlRenderer>
Expand Down Expand Up @@ -146,8 +150,32 @@ public void UrlPresentRenderNonEmpty_ExcludeHost_IncludeQueryString()

Assert.Equal("http:///Test.asp?t=1", result);
}
#if ASP_NET_CORE
[Fact]
public void UrlPresentRenderNonEmpty_UseRawTarget()
{
var renderer = CreateRenderer("www.google.com:80", "?t=1", "http", "/Test.asp", rawTarget: "/rawTarget");
renderer.UseRawTarget = true;

private static AspNetRequestUrlRenderer CreateRenderer(string hostBase, string queryString = "", string scheme = "http", string page = "/", string pathBase = "")
string result = renderer.Render(LogEventInfo.CreateNullEvent());

Assert.Equal("http://www.google.com/rawTarget", result);
}

[Fact]
public void UrlPresentRenderNonEmpty_UseRawTarget_IncludeQueryString()
{
var renderer = CreateRenderer("www.google.com:80", "?t=1", "http", "/Test.asp", rawTarget: "/rawTarget");
renderer.UseRawTarget = true;
renderer.IncludeQueryString = true;

string result = renderer.Render(LogEventInfo.CreateNullEvent());

Assert.Equal("http://www.google.com/rawTarget", result);
}
#endif

private static AspNetRequestUrlRenderer CreateRenderer(string hostBase, string queryString = "", string scheme = "http", string page = "/", string pathBase = "", string rawTarget = null)
{
var (renderer, httpContext) = CreateWithHttpContext();

Expand All @@ -160,8 +188,40 @@ private static AspNetRequestUrlRenderer CreateRenderer(string hostBase, string q
httpContext.Request.QueryString.Returns(new QueryString(queryString));
httpContext.Request.Host.Returns(new HostString(hostBase));
httpContext.Request.Scheme.Returns(scheme);

if (rawTarget != null)
{
httpContext.Request.HttpContext.Returns(httpContext);

var httpRequestFeature = new HttpRequestFeatureMock();
httpRequestFeature.RawTarget = rawTarget;
var collection = new FeatureCollection();
collection.Set<IHttpRequestFeature>(httpRequestFeature);
httpContext.Features.Returns(collection);
}
#endif
return renderer;
}

#if ASP_NET_CORE

private class HttpRequestFeatureMock : IHttpRequestFeature
{
#region Implementation of IHttpRequestFeature

public Stream Body { get; set; }
public IHeaderDictionary Headers { get; set; }
public string Method { get; set; }
public string Path { get; set; }
public string PathBase { get; set; }
public string Protocol { get; set; }
public string QueryString { get; set; }
public string RawTarget { get; set; }
public string Scheme { get; set; }

#endregion Implementation of IHttpRequestFeature
}

#endif
}
}

0 comments on commit d01b128

Please sign in to comment.