From 737ca2a0d586bba43d8f32ad03a0ebf5790a4956 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sat, 10 Jun 2023 12:56:40 +0200 Subject: [PATCH] AspNetRequestHasPostedBodyLayoutRenderer - Available for NLog.Web (#952) --- .../Internal/AssemblyExtensionTypes.cs | 2 +- .../Internal/AssemblyExtensionTypes.tt | 1 - .../Internal/AssemblyExtensionTypes.cs | 1 + ...spNetRequestHasPostedBodyLayoutRenderer.cs | 19 ++++++--- ...RequestHasPostedBodyLayoutRendererTests.cs | 42 ++++++++++++------- 5 files changed, 41 insertions(+), 24 deletions(-) rename src/{NLog.Web.AspNetCore => Shared}/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRenderer.cs (79%) rename tests/{NLog.Web.AspNetCore.Tests => Shared}/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRendererTests.cs (58%) diff --git a/src/NLog.Web.AspNetCore/Internal/AssemblyExtensionTypes.cs b/src/NLog.Web.AspNetCore/Internal/AssemblyExtensionTypes.cs index 2edc36fa..2a11b70d 100644 --- a/src/NLog.Web.AspNetCore/Internal/AssemblyExtensionTypes.cs +++ b/src/NLog.Web.AspNetCore/Internal/AssemblyExtensionTypes.cs @@ -56,6 +56,7 @@ public static void RegisterTypes(this NLog.Config.ISetupExtensionsBuilder setupB setupBuilder.RegisterLayoutRenderer("aspnet-request-cookie"); setupBuilder.RegisterLayoutRenderer("aspnet-request-duration"); setupBuilder.RegisterLayoutRenderer("aspnet-request-form"); + setupBuilder.RegisterLayoutRenderer("aspnet-request-has-posted-body"); setupBuilder.RegisterLayoutRenderer("aspnet-request-headers"); setupBuilder.RegisterLayoutRenderer("aspnet-request-host"); setupBuilder.RegisterLayoutRenderer("aspnet-request-method"); @@ -93,7 +94,6 @@ public static void RegisterTypes(this NLog.Config.ISetupExtensionsBuilder setupB setupBuilder.RegisterTarget("AspNetBufferingWrapper"); #if NET5_0_OR_GREATER - setupBuilder.RegisterLayoutRenderer("aspnet-request-has-posted-body"); setupBuilder.RegisterLayoutRenderer("aspnet-request-stream-id"); #endif #if NETCOREAPP3_0_OR_GREATER diff --git a/src/NLog.Web.AspNetCore/Internal/AssemblyExtensionTypes.tt b/src/NLog.Web.AspNetCore/Internal/AssemblyExtensionTypes.tt index 120911c5..a124bf67 100644 --- a/src/NLog.Web.AspNetCore/Internal/AssemblyExtensionTypes.tt +++ b/src/NLog.Web.AspNetCore/Internal/AssemblyExtensionTypes.tt @@ -115,7 +115,6 @@ namespace NLog.Web.Internal #> #if NET5_0_OR_GREATER - setupBuilder.RegisterLayoutRenderer("aspnet-request-has-posted-body"); setupBuilder.RegisterLayoutRenderer("aspnet-request-stream-id"); #endif #if NETCOREAPP3_0_OR_GREATER diff --git a/src/NLog.Web/Internal/AssemblyExtensionTypes.cs b/src/NLog.Web/Internal/AssemblyExtensionTypes.cs index 0e45743e..ac2ca82d 100644 --- a/src/NLog.Web/Internal/AssemblyExtensionTypes.cs +++ b/src/NLog.Web/Internal/AssemblyExtensionTypes.cs @@ -54,6 +54,7 @@ public static void RegisterTypes(this NLog.Config.ISetupExtensionsBuilder setupB setupBuilder.RegisterLayoutRenderer("aspnet-request-cookie"); setupBuilder.RegisterLayoutRenderer("aspnet-request-duration"); setupBuilder.RegisterLayoutRenderer("aspnet-request-form"); + setupBuilder.RegisterLayoutRenderer("aspnet-request-has-posted-body"); setupBuilder.RegisterLayoutRenderer("aspnet-request-headers"); setupBuilder.RegisterLayoutRenderer("aspnet-request-host"); setupBuilder.RegisterLayoutRenderer("aspnet-request-method"); diff --git a/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRenderer.cs similarity index 79% rename from src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRenderer.cs rename to src/Shared/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRenderer.cs index e21414c6..353cc937 100644 --- a/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRenderer.cs @@ -1,8 +1,6 @@ -#if NET5_0_OR_GREATER +using System.Text; using NLog.LayoutRenderers; using NLog.Web.Internal; -using System.Text; -using Microsoft.AspNetCore.Http.Features; namespace NLog.Web.LayoutRenderers { @@ -31,9 +29,18 @@ public class AspNetRequestHasPostedBodyLayoutRenderer : AspNetLayoutRendererBase /// protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { - var requestFeature = HttpContextAccessor.HttpContext.TryGetFeature(); - builder.Append(requestFeature?.CanHaveBody == true ? '1' : '0'); + builder.Append(CanHaveBody() ? '1' : '0'); + } + + private bool CanHaveBody() + { +#if NET5_0_OR_GREATER + var requestFeature = HttpContextAccessor.HttpContext.TryGetFeature(); + return requestFeature?.CanHaveBody == true; +#else + var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest(); + return httpRequest?.ContentLength > 0L; +#endif } } } -#endif diff --git a/tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRendererTests.cs b/tests/Shared/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRendererTests.cs similarity index 58% rename from tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRendererTests.cs rename to tests/Shared/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRendererTests.cs index 0979ba20..6aa8f3dc 100644 --- a/tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRendererTests.cs +++ b/tests/Shared/LayoutRenderers/AspNetRequestHasPostedBodyLayoutRendererTests.cs @@ -1,6 +1,4 @@ -#if NET5_0_OR_GREATER -using NLog.Web.LayoutRenderers; -using Microsoft.AspNetCore.Http.Features; +using NLog.Web.LayoutRenderers; using NSubstitute; using Xunit; @@ -13,14 +11,20 @@ public void TrueTest() { // Arrange var (renderer, httpContext) = CreateWithHttpContext(); - - var bodyDetectionFeature = Substitute.For(); - bodyDetectionFeature.CanHaveBody.Returns(true); - var featureCollection = new FeatureCollection(); - featureCollection.Set(bodyDetectionFeature); +#if NET5_0_OR_GREATER + var bodyDetectionFeature = Substitute.For(); + bodyDetectionFeature.CanHaveBody.Returns(true); + var featureCollection = new Microsoft.AspNetCore.Http.Features.FeatureCollection(); + featureCollection.Set(bodyDetectionFeature); httpContext.Features.Returns(featureCollection); +#elif ASP_NET_CORE + httpContext.Request.ContentLength = 42; +#else + httpContext.Request.ContentLength.Returns(42); +#endif + // Act var result = renderer.Render(new LogEventInfo()); // Assert @@ -33,13 +37,19 @@ public void FalseTest() // Arrange var (renderer, httpContext) = CreateWithHttpContext(); - var bodyDetectionFeature = Substitute.For(); +#if NET5_0_OR_GREATER + var bodyDetectionFeature = Substitute.For(); bodyDetectionFeature.CanHaveBody.Returns(false); - var featureCollection = new FeatureCollection(); - featureCollection.Set(bodyDetectionFeature); - + var featureCollection = new Microsoft.AspNetCore.Http.Features.FeatureCollection(); + featureCollection.Set(bodyDetectionFeature); httpContext.Features.Returns(featureCollection); +#elif ASP_NET_CORE + httpContext.Request.ContentLength = 0; +#else + httpContext.Request.ContentLength.Returns(0); +#endif + // Act var result = renderer.Render(new LogEventInfo()); // Assert @@ -51,8 +61,9 @@ public void NullTest() { // Arrange var (renderer, httpContext) = CreateWithHttpContext(); - - httpContext.Features.Returns(new FeatureCollection()); +#if NET5_0_OR_GREATER + httpContext.Features.Returns(new Microsoft.AspNetCore.Http.Features.FeatureCollection()); +#endif // Act var result = renderer.Render(new LogEventInfo()); // Assert @@ -71,5 +82,4 @@ protected override void NullRendersEmptyString() Assert.Equal("0", result); } } -} -#endif +} \ No newline at end of file