diff --git a/src/NLog.Web/DefaultHttpContextAccessor.cs b/src/NLog.Web/DefaultHttpContextAccessor.cs index b37281ec..68f3c6e5 100644 --- a/src/NLog.Web/DefaultHttpContextAccessor.cs +++ b/src/NLog.Web/DefaultHttpContextAccessor.cs @@ -21,5 +21,9 @@ public HttpContextBase HttpContext } } + internal bool HasActiveHttpContext() + { + return System.Web.HttpContext.Current != null; // Skip allocating HttpContextWrapper + } } } \ No newline at end of file diff --git a/src/Shared/Internal/HttpContextExtensions.cs b/src/Shared/Internal/HttpContextExtensions.cs index 07b566c3..c41029cf 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 DefaultHttpContextAccessor defaultHttpContextAccessor) + return defaultHttpContextAccessor.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..6daa52fa 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;