Skip to content

Commit

Permalink
AspNetLayoutRendererBase - Skip allocating extra HttpContextWrapper (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Dec 28, 2022
1 parent fe0cbca commit 92cafeb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/NLog.Web/DefaultHttpContextAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ public HttpContextBase HttpContext
}
}

internal bool HasActiveHttpContext()
{
return System.Web.HttpContext.Current != null; // Skip allocating HttpContextWrapper
}
}
}
13 changes: 13 additions & 0 deletions src/Shared/Internal/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 2 additions & 9 deletions src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<IHttpContextAccessor>(serviceProvider, loggingConfiguration);
Expand All @@ -54,13 +53,7 @@ internal static IHttpContextAccessor RetrieveHttpContextAccessor(IServiceProvide
/// <param name="logEvent">Logging event.</param>
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;
Expand Down

0 comments on commit 92cafeb

Please sign in to comment.