Skip to content

Commit

Permalink
Added layout render for ASP.NET Core Connection.Id
Browse files Browse the repository at this point in the history
Add TryGetConnection to HttpContextExtensions and update layout renders that use connection to use that instead.
  • Loading branch information
Burak Akgerman committed Jun 8, 2022
1 parent 3c5e983 commit 7152170
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using NLog.LayoutRenderers;
using NLog.Web.Internal;
using System.Text;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
/// ASP.NET request connection id
/// </summary>
/// <remarks>
/// ${aspnet-request-connection-id}
/// </remarks>
[LayoutRenderer("aspnet-request-connection-id")]
public class AspNetRequestConnectionIdLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
/// Renders the ASP.NET connection ID
/// </summary>
/// <param name="builder">The <see cref="StringBuilder" /> to append the rendered data to.</param>
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var connection = HttpContextAccessor.HttpContext.TryGetConnection();
if (connection == null)
{
return;
}

var id = connection.Id;
if(!string.IsNullOrEmpty(id))
{
builder.Append(id);
}
}
}
}
8 changes: 8 additions & 0 deletions src/Shared/Internal/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ internal static HttpResponseBase TryGetResponse(this HttpContextBase context)
}
}
#else
internal static ConnectionInfo TryGetConnection(this HttpContext context)
{
var connection = context?.Connection;
if (connection == null)
InternalLogger.Debug("HttpContext Connection Lookup returned null");
return connection;
}

internal static HttpRequest TryGetRequest(this HttpContext context)
{
var request = context?.Request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
var httpContext = HttpContextAccessor.HttpContext;

#if ASP_NET_CORE
var connection = httpContext.Connection;
var connection = httpContext.TryGetConnection();
if (connection == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpContext = HttpContextAccessor.HttpContext;
#if ASP_NET_CORE
var connection = httpContext.Connection;
var connection = httpContext.TryGetConnection();
if (connection == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
var httpContext = HttpContextAccessor.HttpContext;

#if ASP_NET_CORE
var connection = httpContext.Connection;
var connection = httpContext.TryGetConnection();
if (connection == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using NLog.Web.LayoutRenderers;
using NSubstitute;
using NSubstitute.ReturnsExtensions;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetRequestConnectionIdLayoutRendererTests : LayoutRenderersTestBase<AspNetRequestConnectionIdLayoutRenderer>
{
[Fact]
public void SuccessTest()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();

httpContext.Connection.Id.Returns("My Connection Id");
// Act
string result = renderer.Render(new LogEventInfo());
// Assert
Assert.Equal("My Connection Id", result);
}

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

httpContext.Connection.Id.Returns("");
// Act
string result = renderer.Render(new LogEventInfo());
// Assert
Assert.Equal("", result);
}

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

httpContext.Connection.Id.ReturnsNull();
// Act
string result = renderer.Render(new LogEventInfo());
// Assert
Assert.Equal("", result);
}
}
}

0 comments on commit 7152170

Please sign in to comment.