Skip to content

Commit

Permalink
Merge pull request #180 from NLog/warning-IHttpContextAccessor
Browse files Browse the repository at this point in the history
Fix IHttpContextAccessor warning
  • Loading branch information
304NotModified authored Sep 29, 2017
2 parents fc7c576 + 2337305 commit 9454c9f
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutRendererBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,18 @@ protected AspNetLayoutRendererBase()
/// <returns>HttpContextAccessor or <c>null</c></returns>
public IHttpContextAccessor HttpContextAccessor
{
get { return _httpContextAccessor ?? (_httpContextAccessor = ServiceLocator.ServiceProvider?.GetService<IHttpContextAccessor>()); }
set { _httpContextAccessor = value; }
get => _httpContextAccessor ?? (_httpContextAccessor = RetrieveHttpContextAccessor());
set => _httpContextAccessor = value;
}

/// <summary>
/// Initialize
/// </summary>
protected override void InitializeLayoutRenderer()
private static IHttpContextAccessor RetrieveHttpContextAccessor()
{
if (HttpContextAccessor == null)
var httpContextAccessor = ServiceLocator.ServiceProvider?.GetService<IHttpContextAccessor>();
if (httpContextAccessor == null)
{
Common.InternalLogger.Warn("Missing IHttpContextAccessor. Has it been registered before loading NLog Configuration? Consider reloading NLog Configuration after having registered the IHttpContextAccessor.");
Common.InternalLogger.Debug("Missing IHttpContextAccessor, so no HttpContext");
}
return httpContextAccessor;
}

#else
Expand Down

0 comments on commit 9454c9f

Please sign in to comment.