From 3494b0c956fcd99abe95f0c67ede52f2ee865a72 Mon Sep 17 00:00:00 2001
From: Julian Verdurmen <304NotModified@users.noreply.github.com>
Date: Sat, 13 May 2017 21:27:55 +0200
Subject: [PATCH] ${Assembly-version} working for asp.net (core) (#66)
* WIP
* adjust patch
* don't patch assemblyversion
* Don't patch test project versions
---
.../AssemblyVersionLayoutRendererTests.cs | 21 ++++
.../NLog.Web.AspNetCore.Tests.csproj | 5 +-
.../AssemblyVersionLayoutRenderer.cs | 101 ++++++++++++++++++
.../AssemblyVersionLayoutRendererTests.cs | 22 ++++
NLog.Web.Tests/NLog.Web.Tests.csproj | 3 +-
.../{AssemblyInfo.cs => AssemblyInfo-test.cs} | 2 +-
NLog.Web/NLog.Web.csproj | 5 +-
.../{AssemblyInfo.cs => AssemblyInfo-test.cs} | 0
appveyor.yml | 2 +-
9 files changed, 153 insertions(+), 8 deletions(-)
create mode 100644 NLog.Web.AspNetCore.Tests/LayoutRenderers/AssemblyVersionLayoutRendererTests.cs
create mode 100644 NLog.Web.AspNetCore/LayoutRenderers/AssemblyVersionLayoutRenderer.cs
create mode 100644 NLog.Web.Tests/AssemblyVersionLayoutRendererTests.cs
rename NLog.Web.Tests/Properties/{AssemblyInfo.cs => AssemblyInfo-test.cs} (97%)
rename NLog.Web/Properties/{AssemblyInfo.cs => AssemblyInfo-test.cs} (100%)
diff --git a/NLog.Web.AspNetCore.Tests/LayoutRenderers/AssemblyVersionLayoutRendererTests.cs b/NLog.Web.AspNetCore.Tests/LayoutRenderers/AssemblyVersionLayoutRendererTests.cs
new file mode 100644
index 00000000..9c339532
--- /dev/null
+++ b/NLog.Web.AspNetCore.Tests/LayoutRenderers/AssemblyVersionLayoutRendererTests.cs
@@ -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);
+ }
+ }
+}
diff --git a/NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj b/NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj
index 309292fa..109ac3f1 100644
--- a/NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj
+++ b/NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj
@@ -4,6 +4,7 @@
net452
$(DefineConstants);NETSTANDARD_1plus
NLog.Web.AspNetCore.Tests
+ 1.2.3.0
@@ -23,11 +24,11 @@
-
+
-
+
diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AssemblyVersionLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AssemblyVersionLayoutRenderer.cs
new file mode 100644
index 00000000..0aefa99d
--- /dev/null
+++ b/NLog.Web.AspNetCore/LayoutRenderers/AssemblyVersionLayoutRenderer.cs
@@ -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
+{
+ ///
+ /// Overwrite the NLog.LayoutRenderers.AssemblyVersionLayoutRenderer
+ ///
+ [LayoutRenderer("assembly-version")]
+ public class AssemblyVersionLayoutRenderer : LayoutRenderer
+ {
+ ///
+ /// The (full) name of the assembly. If null, using the entry assembly.
+ ///
+ [DefaultParameter]
+ public string Name { get; set; }
+
+ ///
+ /// Implemented by subclasses to render request information and append it to the specified .
+ ///
+ /// Won't be called if of is null.
+ ///
+ /// The to append the rendered data to.
+ /// Logging event.
+ 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
+ }
+}
diff --git a/NLog.Web.Tests/AssemblyVersionLayoutRendererTests.cs b/NLog.Web.Tests/AssemblyVersionLayoutRendererTests.cs
new file mode 100644
index 00000000..44fbb71b
--- /dev/null
+++ b/NLog.Web.Tests/AssemblyVersionLayoutRendererTests.cs
@@ -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);
+ }
+ }
+}
diff --git a/NLog.Web.Tests/NLog.Web.Tests.csproj b/NLog.Web.Tests/NLog.Web.Tests.csproj
index f2ef0aea..84bd02f1 100644
--- a/NLog.Web.Tests/NLog.Web.Tests.csproj
+++ b/NLog.Web.Tests/NLog.Web.Tests.csproj
@@ -99,7 +99,8 @@
TestBase.cs
-
+
+
diff --git a/NLog.Web.Tests/Properties/AssemblyInfo.cs b/NLog.Web.Tests/Properties/AssemblyInfo-test.cs
similarity index 97%
rename from NLog.Web.Tests/Properties/AssemblyInfo.cs
rename to NLog.Web.Tests/Properties/AssemblyInfo-test.cs
index b3b7158d..53c2abfb 100644
--- a/NLog.Web.Tests/Properties/AssemblyInfo.cs
+++ b/NLog.Web.Tests/Properties/AssemblyInfo-test.cs
@@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyVersion("1.2.3.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
diff --git a/NLog.Web/NLog.Web.csproj b/NLog.Web/NLog.Web.csproj
index c4d7e9ac..966146b1 100644
--- a/NLog.Web/NLog.Web.csproj
+++ b/NLog.Web/NLog.Web.csproj
@@ -61,7 +61,6 @@
-
@@ -71,8 +70,8 @@
+
-
@@ -87,7 +86,7 @@
-
+
diff --git a/NLog.Web/Properties/AssemblyInfo.cs b/NLog.Web/Properties/AssemblyInfo-test.cs
similarity index 100%
rename from NLog.Web/Properties/AssemblyInfo.cs
rename to NLog.Web/Properties/AssemblyInfo-test.cs
diff --git a/appveyor.yml b/appveyor.yml
index 91238d1f..bd1521f5 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -5,7 +5,7 @@ image: Visual Studio 2017
platform: Any CPU
assembly_info:
patch: true
- file: '**\AssemblyInfo.*'
+ file: '**\AssemblyInfo.cs'
assembly_version: '4.0.0'
assembly_file_version: '{version}'
assembly_informational_version: '4.5' #NLog.Web