From 484cfa7d77c4c821b6c69697f9f3c400aea04156 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Wed, 28 Dec 2022 14:09:29 +0100 Subject: [PATCH] AspNetLayoutRendererBase - Skip allocating extra HttpContextWrapper --- src/NLog.Web/DefaultHttpContextAccessor.cs | 6 +++++- src/NLog.Web/IHttpContextAccessor.cs | 5 +++++ src/Shared/Internal/HttpContextExtensions.cs | 13 +++++++++++++ .../LayoutRenderers/AspNetLayoutRendererBase.cs | 11 ++--------- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/NLog.Web/DefaultHttpContextAccessor.cs b/src/NLog.Web/DefaultHttpContextAccessor.cs index b37281ec..47625d83 100644 --- a/src/NLog.Web/DefaultHttpContextAccessor.cs +++ b/src/NLog.Web/DefaultHttpContextAccessor.cs @@ -5,7 +5,7 @@ namespace NLog.Web /// /// Provides the HttpContext associated with the current request. /// - public class DefaultHttpContextAccessor : IHttpContextAccessor + public class DefaultHttpContextAccessor : IHttpContextAccessor, IHttpContextAccessorValidation { /// /// HttpContext of the current request. @@ -21,5 +21,9 @@ public HttpContextBase HttpContext } } + bool IHttpContextAccessorValidation.HasActiveHttpContext() + { + return System.Web.HttpContext.Current != null; // Skip allocating HttpContextWrapper + } } } \ No newline at end of file diff --git a/src/NLog.Web/IHttpContextAccessor.cs b/src/NLog.Web/IHttpContextAccessor.cs index b55655df..47cc521a 100644 --- a/src/NLog.Web/IHttpContextAccessor.cs +++ b/src/NLog.Web/IHttpContextAccessor.cs @@ -14,4 +14,9 @@ public interface IHttpContextAccessor /// HttpContextBase HttpContext { get; } } + + internal interface IHttpContextAccessorValidation + { + bool HasActiveHttpContext(); + } } \ No newline at end of file diff --git a/src/Shared/Internal/HttpContextExtensions.cs b/src/Shared/Internal/HttpContextExtensions.cs index 07b566c3..f6801384 100644 --- a/src/Shared/Internal/HttpContextExtensions.cs +++ b/src/Shared/Internal/HttpContextExtensions.cs @@ -17,6 +17,14 @@ namespace NLog.Web.Internal internal static class HttpContextExtensions { #if !ASP_NET_CORE + internal static bool HasActiveHttpContext(this IHttpContextAccessor httpContextAccessor) + { + if (httpContextAccessor is IHttpContextAccessorValidation httpContextAccessorValidation) + return httpContextAccessorValidation.HasActiveHttpContext(); // Skip allocating HttpContextWrapper + else + return httpContextAccessor?.HttpContext != null; + } + internal static HttpRequestBase TryGetRequest(this HttpContextBase context) { try @@ -49,6 +57,11 @@ internal static HttpResponseBase TryGetResponse(this HttpContextBase context) } } #else + internal static bool HasActiveHttpContext(this IHttpContextAccessor httpContextAccessor) + { + return httpContextAccessor?.HttpContext != null; + } + internal static WebSocketManager TryGetWebSocket(this HttpContext context) { var websocket = context?.WebSockets; diff --git a/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs b/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs index a6ad541f..82df01d8 100644 --- a/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs +++ b/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs @@ -1,10 +1,10 @@ using System; -using System.Linq; using System.Text; using NLog.Common; using NLog.Config; using NLog.LayoutRenderers; +using NLog.Web.Internal; #if ASP_NET_CORE using Microsoft.AspNetCore.Http; using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext; @@ -40,7 +40,6 @@ public IHttpContextAccessor HttpContextAccessor internal static IHttpContextAccessor DefaultHttpContextAccessor { get; set; } = new DefaultHttpContextAccessor(); internal static IHttpContextAccessor RetrieveHttpContextAccessor(IServiceProvider serviceProvider, LoggingConfiguration loggingConfiguration) => DefaultHttpContextAccessor; #else - internal static IHttpContextAccessor RetrieveHttpContextAccessor(IServiceProvider serviceProvider, LoggingConfiguration loggingConfiguration) { return ServiceLocator.ResolveService(serviceProvider, loggingConfiguration); @@ -54,13 +53,7 @@ internal static IHttpContextAccessor RetrieveHttpContextAccessor(IServiceProvide /// Logging event. protected override void Append(StringBuilder builder, LogEventInfo logEvent) { - var httpContextAccessor = HttpContextAccessor; - if (httpContextAccessor == null) - { - return; - } - - if (httpContextAccessor.HttpContext == null) + if (!httpContextAccessor.HasActiveHttpContext()) { InternalLogger.Debug("No available HttpContext, because outside valid request context. Logger: {0}", logEvent.LoggerName); return;