Skip to content

Commit

Permalink
${Assembly-version} working for asp.net (core) (#66)
Browse files Browse the repository at this point in the history
* WIP

* adjust patch

* don't patch assemblyversion

* Don't patch test project versions
  • Loading branch information
304NotModified authored May 13, 2017
1 parent e234fc4 commit 3494b0c
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 8 deletions.
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);
}
}
}
5 changes: 3 additions & 2 deletions NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFrameworks>net452</TargetFrameworks>
<DefineConstants>$(DefineConstants);NETSTANDARD_1plus</DefineConstants>
<AssemblyName>NLog.Web.AspNetCore.Tests</AssemblyName>
<AssemblyVersion>1.2.3.0</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -23,11 +24,11 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
<Folder Include="Properties\" />
</ItemGroup>


Expand Down
101 changes: 101 additions & 0 deletions NLog.Web.AspNetCore/LayoutRenderers/AssemblyVersionLayoutRenderer.cs
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
}
}
22 changes: 22 additions & 0 deletions NLog.Web.Tests/AssemblyVersionLayoutRendererTests.cs
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);
}
}
}
3 changes: 2 additions & 1 deletion NLog.Web.Tests/NLog.Web.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
<Compile Include="..\NLog.Web.AspNetCore.Tests\TestBase.cs">
<Link>TestBase.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="AssemblyVersionLayoutRendererTests.cs" />
<Compile Include="Properties\AssemblyInfo-test.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NLog.Web\NLog.Web.csproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
5 changes: 2 additions & 3 deletions NLog.Web/NLog.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetItemValueLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetLayoutRendererBase.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetLayoutMultiValueRendererBase.cs" />

<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetMvcControllerRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetMvcActionRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetMvcLayoutRendererBase.cs" />
Expand All @@ -71,8 +70,8 @@
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetSessionValueLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetUserAuthTypeLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetUserIdentityLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AssemblyVersionLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetUserIsAuthenticatedLayoutRenderer.cs" />

<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\IISInstanceNameLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestReferrerRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestUrlRenderer.cs" />
Expand All @@ -87,7 +86,7 @@
<Compile Include="..\NLog.Web.AspNetCore\Targets\AspNetTrace.cs" />
<Compile Include="..\NLog.Web.AspNetCore\Targets\Wrappers\AspNetBufferingTargetWrapper.cs" />
<Compile Include="..\NLog.Web.AspNetCore\Internal\RequestAccessorExtension.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\AssemblyInfo-test.cs" />
</ItemGroup>
<ItemGroup>
<None Include="NLog.snk" />
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3494b0c

Please sign in to comment.