diff --git a/src/Shared/LayoutRenderers/AssemblyVersionLayoutRenderer.cs b/src/Shared/LayoutRenderers/AssemblyVersionLayoutRenderer.cs index f6e2e62c..67483ea6 100644 --- a/src/Shared/LayoutRenderers/AssemblyVersionLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AssemblyVersionLayoutRenderer.cs @@ -1,6 +1,4 @@ -using System; -using System.Linq; -using System.Reflection; +using System.Reflection; using System.Text; using NLog.Common; using NLog.Config; @@ -16,6 +14,14 @@ namespace NLog.Web.LayoutRenderers [ThreadSafe] public class AssemblyVersionLayoutRenderer : NLog.LayoutRenderers.AssemblyVersionLayoutRenderer { +#if !ASP_NET_CORE + /// + /// Support capture of Assembly-Version from active HttpContext + /// + public LayoutRenderer ThreadAgnostic => string.IsNullOrEmpty(Name) ? _threadAgnostic : null; + private readonly LayoutRenderer _threadAgnostic = new ThreadIdLayoutRenderer(); +#endif + /// protected override void Append(StringBuilder builder, LogEventInfo logEvent) { @@ -28,33 +34,25 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent) protected override Assembly GetAssembly() { var assembly = base.GetAssembly(); - #if !ASP_NET_CORE if (assembly == null) { assembly = GetAspNetEntryAssembly(); } #endif - return assembly; } #if !ASP_NET_CORE - private static System.Reflection.Assembly GetAspNetEntryAssembly() + private static Assembly GetAspNetEntryAssembly() { - if (System.Web.HttpContext.Current == null || System.Web.HttpContext.Current.ApplicationInstance == null) + var applicatonType = System.Web.HttpContext.Current?.ApplicationInstance?.GetType(); + while (applicatonType != null && applicatonType.Namespace == "ASP") { - return null; + applicatonType = applicatonType.BaseType; } - - var type = System.Web.HttpContext.Current.ApplicationInstance.GetType(); - while (type != null && type.Namespace == "ASP") - { - type = type.BaseType; - } - return type != null ? type.Assembly : null; + return applicatonType?.Assembly; } - #endif } } \ No newline at end of file diff --git a/tests/Shared/LayoutRenderers/AssemblyVersionLayoutRendererTests.cs b/tests/Shared/LayoutRenderers/AssemblyVersionLayoutRendererTests.cs index e2aa5c06..162440d2 100644 --- a/tests/Shared/LayoutRenderers/AssemblyVersionLayoutRendererTests.cs +++ b/tests/Shared/LayoutRenderers/AssemblyVersionLayoutRendererTests.cs @@ -5,7 +5,7 @@ namespace NLog.Web.Tests.LayoutRenderers { - public class AssemblyVersionLayoutRendererTests : TestBase + public class AssemblyVersionLayoutRendererTests : TestInvolvingAspNetHttpContext { #if ASP_NET_CORE private const string AssemblyName = "NLog.Web.AspNetCore.Tests"; @@ -42,5 +42,33 @@ public void AssemblyNameInformationalVersionTest() Assert.Equal("1.2.3.2", result); } + +#if !ASP_NET_CORE + class TestAplication : System.Web.HttpApplication + { + } + + [Fact] + public void HttpContextAssemblyNameVersionTest() + { + HttpContext.ApplicationInstance = new TestAplication(); + + var logFactory = new LogFactory().Setup().SetupExtensions(evt => evt.RegisterAssembly(typeof(IHttpContextAccessor).Assembly)).LoadConfigurationFromXml(@" + + + + + + + + + ").LogFactory; + var target = logFactory.Configuration.AllTargets.OfType().First(); + var logger = logFactory.GetCurrentClassLogger(); + logger.Info("Hello"); + logFactory.Flush(); + Assert.Equal("1.2.3.0", target.Logs.First()); + } +#endif } } diff --git a/tests/Shared/LayoutRenderers/TestInvolvingAspNetHttpContext.cs b/tests/Shared/LayoutRenderers/TestInvolvingAspNetHttpContext.cs index 368b6d68..01664f9f 100644 --- a/tests/Shared/LayoutRenderers/TestInvolvingAspNetHttpContext.cs +++ b/tests/Shared/LayoutRenderers/TestInvolvingAspNetHttpContext.cs @@ -1,25 +1,15 @@ using System; -using System.Collections; using System.IO; -using System.Reflection; #if !ASP_NET_CORE using System.Web; -using System.Web.Routing; -using System.Collections.Specialized; -using System.Web.SessionState; #else -using Microsoft.Extensions.Primitives; -using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext; using HttpContext = Microsoft.AspNetCore.Http.HttpContext; using Microsoft.AspNetCore.Http; using NSubstitute; #endif using System.Xml; - using NLog.Config; -using Xunit; - namespace NLog.Web.Tests.LayoutRenderers { public abstract class TestInvolvingAspNetHttpContext : TestBase, IDisposable @@ -108,17 +98,5 @@ protected virtual HttpResponse SetUpHttpResponse(HttpContext context) } #endif - - protected NLog.Targets.DebugTarget GetDebugTarget(string targetName, LoggingConfiguration configuration) - { - var debugTarget = (NLog.Targets.DebugTarget)configuration.FindTargetByName(targetName); - Assert.NotNull(debugTarget); - return debugTarget; - } - - protected string GetDebugLastMessage(string targetName, LoggingConfiguration configuration) - { - return GetDebugTarget(targetName, configuration).LastMessage; - } } } \ No newline at end of file