Skip to content

Commit

Permalink
AspNetRequestHasPostedBodyLayoutRenderer - Available for NLog.Web (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Jun 10, 2023
1 parent ea59644 commit 737ca2a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/NLog.Web.AspNetCore/Internal/AssemblyExtensionTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static void RegisterTypes(this NLog.Config.ISetupExtensionsBuilder setupB
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestCookieLayoutRenderer>("aspnet-request-cookie");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestDurationLayoutRenderer>("aspnet-request-duration");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestFormLayoutRenderer>("aspnet-request-form");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestHasPostedBodyLayoutRenderer>("aspnet-request-has-posted-body");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestHeadersLayoutRenderer>("aspnet-request-headers");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestHostLayoutRenderer>("aspnet-request-host");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestHttpMethodRenderer>("aspnet-request-method");
Expand Down Expand Up @@ -93,7 +94,6 @@ public static void RegisterTypes(this NLog.Config.ISetupExtensionsBuilder setupB
setupBuilder.RegisterTarget<NLog.Web.Targets.Wrappers.AspNetBufferingTargetWrapper>("AspNetBufferingWrapper");

#if NET5_0_OR_GREATER
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestHasPostedBodyLayoutRenderer>("aspnet-request-has-posted-body");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestStreamIdLayoutRenderer>("aspnet-request-stream-id");
#endif
#if NETCOREAPP3_0_OR_GREATER
Expand Down
1 change: 0 additions & 1 deletion src/NLog.Web.AspNetCore/Internal/AssemblyExtensionTypes.tt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ namespace NLog.Web.Internal
#>

#if NET5_0_OR_GREATER
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestHasPostedBodyLayoutRenderer>("aspnet-request-has-posted-body");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestStreamIdLayoutRenderer>("aspnet-request-stream-id");
#endif
#if NETCOREAPP3_0_OR_GREATER
Expand Down
1 change: 1 addition & 0 deletions src/NLog.Web/Internal/AssemblyExtensionTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static void RegisterTypes(this NLog.Config.ISetupExtensionsBuilder setupB
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestCookieLayoutRenderer>("aspnet-request-cookie");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestDurationLayoutRenderer>("aspnet-request-duration");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestFormLayoutRenderer>("aspnet-request-form");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestHasPostedBodyLayoutRenderer>("aspnet-request-has-posted-body");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestHeadersLayoutRenderer>("aspnet-request-headers");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestHostLayoutRenderer>("aspnet-request-host");
setupBuilder.RegisterLayoutRenderer<NLog.Web.LayoutRenderers.AspNetRequestHttpMethodRenderer>("aspnet-request-method");
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -31,9 +29,18 @@ public class AspNetRequestHasPostedBodyLayoutRenderer : AspNetLayoutRendererBase
/// <inheritdoc/>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var requestFeature = HttpContextAccessor.HttpContext.TryGetFeature<IHttpRequestBodyDetectionFeature>();
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<Microsoft.AspNetCore.Http.Features.IHttpRequestBodyDetectionFeature>();
return requestFeature?.CanHaveBody == true;
#else
var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
return httpRequest?.ContentLength > 0L;
#endif
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,14 +11,20 @@ public void TrueTest()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();

var bodyDetectionFeature = Substitute.For<IHttpRequestBodyDetectionFeature>();
bodyDetectionFeature.CanHaveBody.Returns(true);

var featureCollection = new FeatureCollection();
featureCollection.Set<IHttpRequestBodyDetectionFeature>(bodyDetectionFeature);
#if NET5_0_OR_GREATER
var bodyDetectionFeature = Substitute.For<Microsoft.AspNetCore.Http.Features.IHttpRequestBodyDetectionFeature>();
bodyDetectionFeature.CanHaveBody.Returns(true);

var featureCollection = new Microsoft.AspNetCore.Http.Features.FeatureCollection();
featureCollection.Set<Microsoft.AspNetCore.Http.Features.IHttpRequestBodyDetectionFeature>(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
Expand All @@ -33,13 +37,19 @@ public void FalseTest()
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();

var bodyDetectionFeature = Substitute.For<IHttpRequestBodyDetectionFeature>();
#if NET5_0_OR_GREATER
var bodyDetectionFeature = Substitute.For<Microsoft.AspNetCore.Http.Features.IHttpRequestBodyDetectionFeature>();
bodyDetectionFeature.CanHaveBody.Returns(false);

var featureCollection = new FeatureCollection();
featureCollection.Set<IHttpRequestBodyDetectionFeature>(bodyDetectionFeature);

var featureCollection = new Microsoft.AspNetCore.Http.Features.FeatureCollection();
featureCollection.Set<Microsoft.AspNetCore.Http.Features.IHttpRequestBodyDetectionFeature>(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
Expand All @@ -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
Expand All @@ -71,5 +82,4 @@ protected override void NullRendersEmptyString()
Assert.Equal("0", result);
}
}
}
#endif
}

0 comments on commit 737ca2a

Please sign in to comment.