-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
${Assembly-version} working for asp.net (core) (#66)
* WIP * adjust patch * don't patch assemblyversion * Don't patch test project versions
- Loading branch information
1 parent
e234fc4
commit 3494b0c
Showing
9 changed files
with
153 additions
and
8 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
NLog.Web.AspNetCore.Tests/LayoutRenderers/AssemblyVersionLayoutRendererTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using NLog.Layouts; | ||
using NLog.Web.Tests.LayoutRenderers; | ||
using Xunit; | ||
|
||
namespace NLog.Web.AspNetCore.Tests.LayoutRenderers | ||
{ | ||
public class AssemblyVersionLayoutRendererTests : TestBase | ||
{ | ||
[Fact] | ||
public void AssemblyNameVersionTest() | ||
{ | ||
Layout l = "${assembly-version:NLog.Web.AspNetCore.Tests}"; | ||
var result = l.Render(LogEventInfo.CreateNullEvent()); | ||
Assert.Equal("1.2.3.0", result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
NLog.Web.AspNetCore/LayoutRenderers/AssemblyVersionLayoutRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using NLog.Common; | ||
using NLog.Config; | ||
using NLog.LayoutRenderers; | ||
|
||
#if NETSTANDARD_1plus | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.PlatformAbstractions; | ||
#endif | ||
|
||
namespace NLog.Web.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// Overwrite the NLog.LayoutRenderers.AssemblyVersionLayoutRenderer | ||
/// </summary> | ||
[LayoutRenderer("assembly-version")] | ||
public class AssemblyVersionLayoutRenderer : LayoutRenderer | ||
{ | ||
/// <summary> | ||
/// The (full) name of the assembly. If <c>null</c>, using the entry assembly. | ||
/// </summary> | ||
[DefaultParameter] | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// Implemented by subclasses to render request information and append it to the specified <see cref="StringBuilder" />. | ||
/// | ||
/// Won't be called if <see cref="AspNetLayoutRendererBase.HttpContextAccessor"/> of <see cref="IHttpContextAccessor.HttpContext"/> is <c>null</c>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param> | ||
/// <param name="logEvent">Logging event.</param> | ||
protected override void Append(StringBuilder builder, LogEventInfo logEvent) | ||
{ | ||
InternalLogger.Trace("Using ${assembly-version} of NLog.Web"); | ||
|
||
var nameNotEmpty = !string.IsNullOrEmpty(Name); | ||
if (nameNotEmpty) | ||
{ | ||
var assembly = Assembly.Load(new AssemblyName(Name)); | ||
if (assembly == null) | ||
{ | ||
builder.Append("Could not find assembly " + Name); | ||
} | ||
else | ||
{ | ||
builder.Append(assembly.GetName().Version.ToString()); | ||
} | ||
} | ||
else | ||
{ | ||
//try entry assembly | ||
|
||
#if NETSTANDARD_1plus | ||
string assemblyVersion = PlatformServices.Default.Application.RuntimeFramework.Version.ToString(); | ||
|
||
builder.Append(assemblyVersion); | ||
#else | ||
|
||
var assembly = Assembly.GetEntryAssembly(); | ||
|
||
if (assembly == null) | ||
{ | ||
assembly = GetAspNetEntryAssembly(); | ||
} | ||
if (assembly == null) | ||
{ | ||
builder.Append("Could not entry assembly"); | ||
} | ||
else | ||
{ | ||
builder.Append(assembly.GetName().Version.ToString()); | ||
} | ||
#endif | ||
|
||
} | ||
|
||
} | ||
|
||
#if !NETSTANDARD_1plus | ||
private static Assembly GetAspNetEntryAssembly() | ||
{ | ||
if (System.Web.HttpContext.Current == null || System.Web.HttpContext.Current.ApplicationInstance == null) | ||
{ | ||
return null; | ||
} | ||
var type = System.Web.HttpContext.Current.ApplicationInstance.GetType(); | ||
while (type != null && type.Namespace == "ASP") | ||
{ | ||
type = type.BaseType; | ||
} | ||
return type != null ? type.Assembly : null; | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using NLog.Config; | ||
using NLog.Layouts; | ||
using NLog.Web.Tests.LayoutRenderers; | ||
using Xunit; | ||
|
||
namespace NLog.Web.AspNetCore.Tests.LayoutRenderers | ||
{ | ||
public class AssemblyVersionLayoutRendererTests : TestBase | ||
{ | ||
[Fact] | ||
public void AssemblyNameVersionTest() | ||
{ | ||
Layout l = "${assembly-version:NLog.Web.Tests}"; | ||
var result = l.Render(LogEventInfo.CreateNullEvent()); | ||
Assert.Equal("1.2.3.0", result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters