diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutRendererBase.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutRendererBase.cs index 64a43a1c..cf923238 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutRendererBase.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutRendererBase.cs @@ -38,10 +38,21 @@ protected AspNetLayoutRendererBase() /// HttpContextAccessor or null public IHttpContextAccessor HttpContextAccessor { - get { return _httpContextAccessor ?? ServiceLocator.ServiceProvider?.GetService(); } + get { return _httpContextAccessor ?? (_httpContextAccessor = ServiceLocator.ServiceProvider?.GetService()); } set { _httpContextAccessor = value; } } + /// + /// Initialize + /// + protected override void InitializeLayoutRenderer() + { + 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."); + } + } + #else /// /// Provides access to the current request HttpContext.