Skip to content

Commit

Permalink
Fix AssemblyVersionLayoutRenderer on ASP.NET to work with async=true (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Feb 16, 2021
1 parent 1620a84 commit e414b70
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
30 changes: 14 additions & 16 deletions src/Shared/LayoutRenderers/AssemblyVersionLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Linq;
using System.Reflection;
using System.Reflection;
using System.Text;
using NLog.Common;
using NLog.Config;
Expand All @@ -16,6 +14,14 @@ namespace NLog.Web.LayoutRenderers
[ThreadSafe]
public class AssemblyVersionLayoutRenderer : NLog.LayoutRenderers.AssemblyVersionLayoutRenderer
{
#if !ASP_NET_CORE
/// <summary>
/// Support capture of Assembly-Version from active HttpContext
/// </summary>
public LayoutRenderer ThreadAgnostic => string.IsNullOrEmpty(Name) ? _threadAgnostic : null;
private readonly LayoutRenderer _threadAgnostic = new ThreadIdLayoutRenderer();
#endif

/// <inheritdoc />
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(@"
<nlog throwConfigExceptions='true'>
<targets async='true'>
<target name='memory' type='memory' layout='${assembly-version}' />
</targets>
<rules>
<logger name='*' writeTo='memory' />
</rules>
</nlog>
").LogFactory;
var target = logFactory.Configuration.AllTargets.OfType<NLog.Targets.MemoryTarget>().First();
var logger = logFactory.GetCurrentClassLogger();
logger.Info("Hello");
logFactory.Flush();
Assert.Equal("1.2.3.0", target.Logs.First());
}
#endif
}
}
22 changes: 0 additions & 22 deletions tests/Shared/LayoutRenderers/TestInvolvingAspNetHttpContext.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit e414b70

Please sign in to comment.