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 committed Dec 28, 2022
1 parent fe0cbca commit 484cfa7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/NLog.Web/DefaultHttpContextAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NLog.Web
/// <summary>
/// Provides the HttpContext associated with the current request.
/// </summary>
public class DefaultHttpContextAccessor : IHttpContextAccessor
public class DefaultHttpContextAccessor : IHttpContextAccessor, IHttpContextAccessorValidation
{
/// <summary>
/// HttpContext of the current request.
Expand All @@ -21,5 +21,9 @@ public HttpContextBase HttpContext
}
}

bool IHttpContextAccessorValidation.HasActiveHttpContext()
{
return System.Web.HttpContext.Current != null; // Skip allocating HttpContextWrapper
}
}
}
5 changes: 5 additions & 0 deletions src/NLog.Web/IHttpContextAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public interface IHttpContextAccessor
/// </summary>
HttpContextBase HttpContext { get; }
}

internal interface IHttpContextAccessorValidation
{
bool HasActiveHttpContext();
}
}
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 IHttpContextAccessorValidation httpContextAccessorValidation)
return httpContextAccessorValidation.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 484cfa7

Please sign in to comment.