Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aspnet-request-endpoint - EndPoint Routing DisplayName #936

Merged
merged 1 commit into from
May 19, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if NETCOREAPP3_0_OR_GREATER

using System;
using System.Text;
using NLog.LayoutRenderers;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
/// ASP.NET request EndPoint DisplayName
/// </summary>
/// <remarks>
/// <code>${aspnet-request-endpoint}</code>
/// </remarks>
/// <seealso href="https://github.com/NLog/NLog/wiki/AspNet-Request-EndPoint-Layout-Renderer">Documentation on NLog Wiki</seealso>
[LayoutRenderer("aspnet-request-endpoint")]
public class AspNetRequestEndPointLayoutRenderer : AspNetLayoutRendererBase
{
/// <inheritdoc/>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var endPoint = HttpContextAccessor.HttpContext?.Features?.Get<Microsoft.AspNetCore.Http.Features.IEndpointFeature>()?.Endpoint;
builder.Append(endPoint?.DisplayName);
}
}
}


#endif
2 changes: 1 addition & 1 deletion src/Shared/Internal/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ internal static ISession TryGetSession(this HttpContext context)
{
try
{
if (context?.Features.Get<ISessionFeature>()?.Session != null)
if (context?.Features?.Get<ISessionFeature>()?.Session != null)
{
var session = context?.Session;
if (session == null)
Expand Down
3 changes: 1 addition & 2 deletions src/Shared/LayoutRenderers/AspNetRequestUrlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ private void RenderUrl(HttpRequest httpRequest, StringBuilder builder)

if (HasPropertiesFlag(AspNetRequestUrlProperty.Path))
{
IHttpRequestFeature httpRequestFeature;
if (UseRawTarget && (httpRequestFeature = httpRequest.HttpContext.Features.Get<IHttpRequestFeature>()) != null)
if (UseRawTarget && httpRequest.HttpContext?.Features?.Get<IHttpRequestFeature>() is IHttpRequestFeature httpRequestFeature)
{
builder.Append(httpRequestFeature.RawTarget);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#if NETCOREAPP3_0_OR_GREATER
using NLog.Web.LayoutRenderers;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.AspNetCore.Http.Features;
using NSubstitute;
using Xunit;

namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetRequestEndPointLayoutRendererTests : LayoutRenderersTestBase<AspNetRequestEndPointLayoutRenderer>
{
[Fact]
public void SuccessTest()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
var endPointFeature = Substitute.For<IEndpointFeature>();
endPointFeature.Endpoint.Returns(new Microsoft.AspNetCore.Http.Endpoint(null, null, "42"));

var featureCollection = new FeatureCollection();
featureCollection.Set<IEndpointFeature>(endPointFeature);

httpContext.Features.Returns(featureCollection);
// Act
var result = renderer.Render(new LogEventInfo());
// Assert
Assert.Equal("42", result);
}

[Fact]
public void NullTest()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
httpContext.Features.Returns(new FeatureCollection());
// Act
var result = renderer.Render(new LogEventInfo());
// Assert
Assert.Equal(string.Empty, result);
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ public void EmptyVarname()
[Fact]
public void SessionWithCulture()
{
LayoutRenderer.Register<AspNetSessionValueLayoutRenderer>("aspnet-session");
Layout layout = "${aspnet-session:a.b:culture=en-GB:evaluateAsNestedProperties=true}";
Layout layout = null;

var logFactory = new LogFactory().Setup().SetupExtensions(ext => ext.RegisterLayoutRenderer<AspNetSessionValueLayoutRenderer>("aspnet-session")).LoadConfiguration(c =>
{
layout = "${aspnet-session:a.b:culture=en-GB:evaluateAsNestedProperties=true}";
});

var o = new { b = new DateTime(2015, 11, 24, 2, 30, 23) };
//set in "a"
Expand Down