forked from NLog/NLog.Web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added layout render for ASP.NET Core Connection.Id
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
Showing
6 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestConnectionIdLayoutRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestConnectionIdLayoutRendererTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |