diff --git a/src/NLog.Web/Properties/AssemblyInfo.cs b/src/NLog.Web/Properties/AssemblyInfo.cs
index bbae35408..296b54b7c 100644
--- a/src/NLog.Web/Properties/AssemblyInfo.cs
+++ b/src/NLog.Web/Properties/AssemblyInfo.cs
@@ -9,3 +9,4 @@
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("74d5915b-bea9-404c-b4d0-b663164def37")]
+[assembly: InternalsVisibleTo("NLog.Web.Tests,PublicKey=0024000004800000940000000602000000240000525341310004000001000100772391e63c104728adcf18e2390474262559fa7f34a4215848f43288cde875dcc92a06222e9be0592b211ff74adbb5d21a7aab5522b540b1735f2f03279221056fedbe7e534073dabee9db48f8ecebcf1dc98a95576e45cbeff5fe7c4842859451ab2dae7a8370f1b2f7a529d2ca210e3e844d973523d73d193df6c17f1314a6")]
diff --git a/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs b/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs
index b95dd34a9..dd6b98f7c 100644
--- a/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs
+++ b/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs
@@ -18,28 +18,12 @@ namespace NLog.Web.LayoutRenderers
///
public abstract class AspNetLayoutRendererBase : LayoutRenderer
{
- ///
- /// Initializes the .
- ///
- protected AspNetLayoutRendererBase()
- {
-#if !ASP_NET_CORE
- HttpContextAccessor = RetrieveHttpContextAccessor();
-#endif
- }
-
-
-#if !ASP_NET_CORE
- private static IHttpContextAccessor RetrieveHttpContextAccessor() => new DefaultHttpContextAccessor();
-#endif
-
-#if ASP_NET_CORE
-
///
/// Context for DI
///
private IHttpContextAccessor _httpContextAccessor;
+
///
/// Provides access to the current request HttpContext.
///
@@ -51,6 +35,13 @@ public IHttpContextAccessor HttpContextAccessor
set => _httpContextAccessor = value;
}
+#if !ASP_NET_CORE
+
+ internal static IHttpContextAccessor DefaultHttpContextAccessor { get; set; } = new DefaultHttpContextAccessor();
+
+ private static IHttpContextAccessor RetrieveHttpContextAccessor() => DefaultHttpContextAccessor;
+#else
+
private static IHttpContextAccessor RetrieveHttpContextAccessor()
{
var serviceProvider = ServiceLocator.ServiceProvider;
@@ -77,12 +68,7 @@ private static IHttpContextAccessor RetrieveHttpContextAccessor()
}
}
-#else
- ///
- /// Provides access to the current request HttpContext.
- ///
- [NLog.Config.NLogConfigurationIgnorePropertyAttribute]
- public IHttpContextAccessor HttpContextAccessor { get; set; }
+
#endif
diff --git a/tests/Shared/RegisterCustomLayoutRenderer.cs b/tests/Shared/RegisterCustomLayoutRenderer.cs
index 7ff1b3949..e1e1a5161 100644
--- a/tests/Shared/RegisterCustomLayoutRenderer.cs
+++ b/tests/Shared/RegisterCustomLayoutRenderer.cs
@@ -21,6 +21,13 @@ namespace NLog.Web.AspNetCore.Tests
{
public class RegisterCustomLayoutRenderer : TestBase
{
+#if !ASP_NET_CORE
+ ~RegisterCustomLayoutRenderer()
+ {
+ AspNetLayoutRendererBase.DefaultHttpContextAccessor = new DefaultHttpContextAccessor();
+ }
+#endif
+
[Fact]
public void RegisterLayoutRendererTest()
{
@@ -29,6 +36,7 @@ public void RegisterLayoutRendererTest()
httpcontext.Connection.LocalPort.Returns(123);
#else
httpcontext.Request.RawUrl.Returns("123");
+
#endif
// Act
@@ -56,14 +64,17 @@ private static
SetupHttpAccessorWithHttpContext()
{
var httpContextAccessorMock = Substitute.For();
- var serviceProviderMock = Substitute.For();
- serviceProviderMock.GetService(typeof(IHttpContextAccessor)).Returns(httpContextAccessorMock);
+
#if ASP_NET_CORE
+ var serviceProviderMock = Substitute.For();
+ serviceProviderMock.GetService(typeof(IHttpContextAccessor)).Returns(httpContextAccessorMock);
var httpcontext = Substitute.For();
ServiceLocator.ServiceProvider = serviceProviderMock;
#else
var httpcontext = Substitute.For();
+ httpContextAccessorMock.HttpContext.Returns(httpcontext);
+ AspNetLayoutRendererBase.DefaultHttpContextAccessor = httpContextAccessorMock;
#endif