From 3444edd1a95568530975fb69a64588bbcfcc3772 Mon Sep 17 00:00:00 2001 From: Burak Akgerman Date: Wed, 8 Jun 2022 12:17:25 -0400 Subject: [PATCH] Added renders for is form content type and is https --- ...tRequestIsFormContentTypeLayoutRenderer.cs | 27 ++++++++ .../AspNetRequestIsHttpsLayoutRenderer.cs | 27 ++++++++ ...estIsFormContentTypeLayoutRendererTests.cs | 64 +++++++++++++++++++ ...AspNetRequestIsHttpsLayoutRendererTests.cs | 64 +++++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestIsFormContentTypeLayoutRenderer.cs create mode 100644 src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestIsHttpsLayoutRenderer.cs create mode 100644 tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestIsFormContentTypeLayoutRendererTests.cs create mode 100644 tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestIsHttpsLayoutRendererTests.cs diff --git a/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestIsFormContentTypeLayoutRenderer.cs b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestIsFormContentTypeLayoutRenderer.cs new file mode 100644 index 00000000..a40f386c --- /dev/null +++ b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestIsFormContentTypeLayoutRenderer.cs @@ -0,0 +1,27 @@ +using NLog.LayoutRenderers; +using NLog.Web.Internal; +using System.Text; + +namespace NLog.Web.LayoutRenderers +{ + /// + /// ASP.NET Is Request Form Content Type + /// + /// + /// ${aspnet-request-is-form-content-type} + /// + [LayoutRenderer("aspnet-request-is-form-content-type")] + public class AspNetRequestIsFormContentTypeLayoutRenderer : AspNetLayoutRendererBase + { + /// + /// Renders the specified ASP.NET Core HttpContext.Request.HasFormContentType variable and appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) + { + var request = HttpContextAccessor.HttpContext?.TryGetRequest(); + builder.Append(request?.HasFormContentType == true ? '1' : '0'); + } + } +} diff --git a/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestIsHttpsLayoutRenderer.cs b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestIsHttpsLayoutRenderer.cs new file mode 100644 index 00000000..b879519f --- /dev/null +++ b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestIsHttpsLayoutRenderer.cs @@ -0,0 +1,27 @@ +using NLog.LayoutRenderers; +using NLog.Web.Internal; +using System.Text; + +namespace NLog.Web.LayoutRenderers +{ + /// + /// ASP.NET Is Request HTTPS + /// + /// + /// ${aspnet-request-is-https} + /// + [LayoutRenderer("aspnet-request-is-https")] + public class AspNetRequestIsHttpsLayoutRenderer : AspNetLayoutRendererBase + { + /// + /// Renders the specified ASP.NET Core HttpContext.Request.IsHttps variable and appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) + { + var request = HttpContextAccessor.HttpContext?.TryGetRequest(); + builder.Append(request?.IsHttps == true ? '1' : '0'); + } + } +} diff --git a/tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestIsFormContentTypeLayoutRendererTests.cs b/tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestIsFormContentTypeLayoutRendererTests.cs new file mode 100644 index 00000000..d9a12318 --- /dev/null +++ b/tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestIsFormContentTypeLayoutRendererTests.cs @@ -0,0 +1,64 @@ +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 AspNetRequestIsFormContentTypeLayoutRendererTests : LayoutRenderersTestBase + { + [Fact] + public void TrueTest() + { + // Arrange + var (renderer, httpContext) = CreateWithHttpContext(); + + httpContext.Request.HasFormContentType.Returns(true); + // Act + string result = renderer.Render(new LogEventInfo()); + // Assert + Assert.Equal("1", result); + } + + [Fact] + public void FalseTest() + { + // Arrange + var (renderer, httpContext) = CreateWithHttpContext(); + + httpContext.Request.HasFormContentType.Returns(false); + // Act + string result = renderer.Render(new LogEventInfo()); + // Assert + Assert.Equal("0", result); + } + + [Fact] + public void NullTest() + { + // Arrange + var (renderer, httpContext) = CreateWithHttpContext(); + + httpContext.Request.ReturnsNull(); + // Act + string result = renderer.Render(new LogEventInfo()); + // Assert + Assert.Equal("0", result); + } + + protected override void NullRendersEmptyString() + { + // Arrange + var (renderer, _) = CreateWithHttpContext(); + + // Act + string result = renderer.Render(LogEventInfo.CreateNullEvent()); + + // Assert + Assert.Equal("0", result); + } + } +} diff --git a/tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestIsHttpsLayoutRendererTests.cs b/tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestIsHttpsLayoutRendererTests.cs new file mode 100644 index 00000000..e6f7541f --- /dev/null +++ b/tests/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetRequestIsHttpsLayoutRendererTests.cs @@ -0,0 +1,64 @@ +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 AspNetRequestIsHttpsLayoutRendererTests : LayoutRenderersTestBase + { + [Fact] + public void TrueTest() + { + // Arrange + var (renderer, httpContext) = CreateWithHttpContext(); + + httpContext.Request.IsHttps.Returns(true); + // Act + string result = renderer.Render(new LogEventInfo()); + // Assert + Assert.Equal("1", result); + } + + [Fact] + public void FalseTest() + { + // Arrange + var (renderer, httpContext) = CreateWithHttpContext(); + + httpContext.Request.IsHttps.Returns(false); + // Act + string result = renderer.Render(new LogEventInfo()); + // Assert + Assert.Equal("0", result); + } + + [Fact] + public void NullTest() + { + // Arrange + var (renderer, httpContext) = CreateWithHttpContext(); + + httpContext.Request.ReturnsNull(); + // Act + string result = renderer.Render(new LogEventInfo()); + // Assert + Assert.Equal("0", result); + } + + protected override void NullRendersEmptyString() + { + // Arrange + var (renderer, _) = CreateWithHttpContext(); + + // Act + string result = renderer.Render(LogEventInfo.CreateNullEvent()); + + // Assert + Assert.Equal("0", result); + } + } +}