Skip to content

Commit

Permalink
AspNetLayoutRendererBase - Added more debug-info when failing to look…
Browse files Browse the repository at this point in the history
…up HttpContext
  • Loading branch information
snakefoot committed Feb 15, 2021
1 parent 1acc578 commit 3065ac4
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,30 @@ public abstract class AspNetLayoutRendererBase : LayoutRenderer
/// </summary>
private IHttpContextAccessor _httpContextAccessor;


/// <summary>
/// Provides access to the current request HttpContext.
/// </summary>
/// <returns>HttpContextAccessor or <c>null</c></returns>
[NLogConfigurationIgnoreProperty]
public IHttpContextAccessor HttpContextAccessor
{
get => _httpContextAccessor ?? (_httpContextAccessor = RetrieveHttpContextAccessor());
get => _httpContextAccessor ?? (_httpContextAccessor = RetrieveHttpContextAccessor(GetType()));
set => _httpContextAccessor = value;
}

#if !ASP_NET_CORE

internal static IHttpContextAccessor DefaultHttpContextAccessor { get; set; } = new DefaultHttpContextAccessor();

private static IHttpContextAccessor RetrieveHttpContextAccessor() => DefaultHttpContextAccessor;
private static IHttpContextAccessor RetrieveHttpContextAccessor(Type _) => DefaultHttpContextAccessor;
#else

private static IHttpContextAccessor RetrieveHttpContextAccessor()
private static IHttpContextAccessor RetrieveHttpContextAccessor(Type classType)
{
var serviceProvider = ServiceLocator.ServiceProvider;
if (serviceProvider == null)
{
InternalLogger.Debug("Missing serviceProvider, so no HttpContext");
InternalLogger.Debug("{0} - Missing serviceProvider, so no HttpContext", classType);
return null;
}

Expand All @@ -59,14 +58,14 @@ private static IHttpContextAccessor RetrieveHttpContextAccessor()
var httpContextAccessor = serviceProvider.GetService<IHttpContextAccessor>();
if (httpContextAccessor == null)
{
InternalLogger.Debug("Missing IHttpContextAccessor, so no HttpContext");
InternalLogger.Debug("{0} - Missing IHttpContextAccessor, so no HttpContext", classType);
}

return httpContextAccessor;
}
catch (ObjectDisposedException ex)
{
InternalLogger.Debug(ex, "ServiceProvider has been disposed, so no HttpContext");
InternalLogger.Debug(ex, "{0} - ServiceProvider has been disposed, so no HttpContext", classType);
return null;
}
}
Expand Down Expand Up @@ -113,15 +112,15 @@ protected override void CloseLayoutRenderer()
}
#endif


/// <summary>
/// Register a custom layout renderer with a callback function <paramref name="func" />. The callback recieves the logEvent and the current configuration.
/// </summary>
/// <param name="name">Name of the layout renderer - without ${}.</param>
/// <param name="func">Callback that returns the value for the layout renderer.</param>
public static void Register(string name, Func<LogEventInfo, HttpContextBase, LoggingConfiguration, object> func)
{
object NewFunc(LogEventInfo logEventInfo, LoggingConfiguration configuration) => func(logEventInfo, RetrieveHttpContextAccessor()?.HttpContext, configuration);
// TODO Missing caching (and cache-reset) of HttpContextAccessor - Constant lookup in ServiceProvider can lead to deadlock situation
object NewFunc(LogEventInfo logEventInfo, LoggingConfiguration configuration) => func(logEventInfo, RetrieveHttpContextAccessor(null)?.HttpContext, configuration);

Register(name, NewFunc);
}
Expand Down

0 comments on commit 3065ac4

Please sign in to comment.