diff --git a/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestEndPointLayoutRenderer.cs b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestEndPointLayoutRenderer.cs new file mode 100644 index 00000000..645ce9ac --- /dev/null +++ b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestEndPointLayoutRenderer.cs @@ -0,0 +1,29 @@ +#if NETCOREAPP3_0_OR_GREATER + +using System; +using System.Text; +using NLog.LayoutRenderers; + +namespace NLog.Web.LayoutRenderers +{ + /// + /// ASP.NET request EndPoint DisplayName + /// + /// + /// ${aspnet-request-endpoint} + /// + /// Documentation on NLog Wiki + [LayoutRenderer("aspnet-request-endpoint")] + public class AspNetRequestEndPointLayoutRenderer : AspNetLayoutRendererBase + { + /// + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) + { + var endPoint = HttpContextAccessor.HttpContext?.Features?.Get()?.Endpoint; + builder.Append(endPoint?.DisplayName); + } + } +} + + +#endif \ No newline at end of file diff --git a/src/Shared/Internal/HttpContextExtensions.cs b/src/Shared/Internal/HttpContextExtensions.cs index ef86ce1b..bbb0d696 100644 --- a/src/Shared/Internal/HttpContextExtensions.cs +++ b/src/Shared/Internal/HttpContextExtensions.cs @@ -152,7 +152,7 @@ internal static ISession TryGetSession(this HttpContext context) { try { - if (context?.Features.Get()?.Session != null) + if (context?.Features?.Get()?.Session != null) { var session = context?.Session; if (session == null) diff --git a/src/Shared/LayoutRenderers/AspNetRequestUrlRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestUrlRenderer.cs index 9072ad73..b0988841 100644 --- a/src/Shared/LayoutRenderers/AspNetRequestUrlRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestUrlRenderer.cs @@ -193,8 +193,7 @@ private void RenderUrl(HttpRequest httpRequest, StringBuilder builder) if (HasPropertiesFlag(AspNetRequestUrlProperty.Path)) { - IHttpRequestFeature httpRequestFeature; - if (UseRawTarget && (httpRequestFeature = httpRequest.HttpContext.Features.Get()) != null) + if (UseRawTarget && httpRequest.HttpContext?.Features?.Get() is IHttpRequestFeature httpRequestFeature) { builder.Append(httpRequestFeature.RawTarget); } diff --git a/tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestEndPointLayoutRendererTests.cs b/tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestEndPointLayoutRendererTests.cs new file mode 100644 index 00000000..aeb6e8e3 --- /dev/null +++ b/tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestEndPointLayoutRendererTests.cs @@ -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 + { + [Fact] + public void SuccessTest() + { + // Arrange + var (renderer, httpContext) = CreateWithHttpContext(); + var endPointFeature = Substitute.For(); + endPointFeature.Endpoint.Returns(new Microsoft.AspNetCore.Http.Endpoint(null, null, "42")); + + var featureCollection = new FeatureCollection(); + featureCollection.Set(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 diff --git a/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests.cs b/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests.cs index c94132db..bd08b41d 100644 --- a/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests.cs +++ b/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests.cs @@ -157,8 +157,12 @@ public void EmptyVarname() [Fact] public void SessionWithCulture() { - LayoutRenderer.Register("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("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"