Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ namespace NLog.LayoutRenderers
[ThreadAgnostic]
public class AssemblyVersionLayoutRenderer : LayoutRenderer
{
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyVersionLayoutRenderer" /> class.
/// </summary>
public AssemblyVersionLayoutRenderer()
{
_format = DefaultFormat;
}

/// <summary>
/// The (full) name of the assembly. If <c>null</c>, using the entry assembly.
/// </summary>
Expand All @@ -77,9 +69,12 @@ public AssemblyVersionLayoutRenderer()
/// <docgen category='Layout Options' order='10' />
public AssemblyVersionType Type { get; set; } = AssemblyVersionType.Assembly;

private const string DefaultFormat = "major.minor.build.revision";

private string _format;
///<summary>
/// The default value to render if the Version is not available
///</summary>
/// <docgen category='Layout Options' order='10' />
public string Default { get => _default ?? GenerateDefaultValue(); set => _default = value; }
private string _default;

/// <summary>
/// Gets or sets the custom format of the assembly version output.
Expand All @@ -94,8 +89,11 @@ public AssemblyVersionLayoutRenderer()
public string Format
{
get => _format;
set => _format = value?.ToLowerInvariant();
set => _format = value?.ToLowerInvariant() ?? string.Empty;
}
private string _format = DefaultFormat;

private const string DefaultFormat = "major.minor.build.revision";

/// <inheritdoc/>
protected override void InitializeLayoutRenderer()
Expand All @@ -117,17 +115,26 @@ protected override void CloseLayoutRenderer()
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
var version = _assemblyVersion ?? (_assemblyVersion = ApplyFormatToVersion(GetVersion()));

if (string.IsNullOrEmpty(version))
{
version = $"Could not find value for {(string.IsNullOrEmpty(Name) ? "entry" : Name)} assembly and version type {Type}";
}

if (version is null)
version = GenerateDefaultValue();
builder.Append(version);
}

private string ApplyFormatToVersion(string version)
{
if (version is null)
{
return _default;
}
else if (StringHelpers.IsNullOrWhiteSpace(version))
{
return _default ?? GenerateDefaultValue();
}
else if (version == "0.0.0.0" && _default != null)
{
return _default;
}

if (Format.Equals(DefaultFormat, StringComparison.OrdinalIgnoreCase) || string.IsNullOrEmpty(version))
{
return version;
Expand All @@ -142,6 +149,11 @@ private string ApplyFormatToVersion(string version)
return version;
}

private string GenerateDefaultValue()
{
return $"Could not find value for {(string.IsNullOrEmpty(Name) ? "entry" : Name)} assembly and version type {Type}";
}

#if NETSTANDARD1_3

private string GetVersion()
Expand All @@ -166,8 +178,18 @@ private System.Reflection.Assembly GetAssembly()

private string GetVersion()
{
var assembly = GetAssembly();
return GetVersion(assembly);
try
{
var assembly = GetAssembly();
return GetVersion(assembly) ?? string.Empty;
}
catch (Exception ex)
{
NLog.Common.InternalLogger.Warn(ex, "${assembly-version} - Failed to load assembly {0}", Name);
if (ex.MustBeRethrown())
throw;
return null;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public enum AssemblyVersionType
File,

/// <summary>
/// Gets additional version information.
/// Gets the product version, extracted from the additional version information.
/// </summary>
Informational
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,30 @@ public void EntryAssemblyVersionTest()
AssertLayoutRendererOutput("${assembly-version}", assemblyVersion);
}

[Fact]
public void EntryAssemblyVersionDefaultTest()
{
var assembly = Assembly.GetEntryAssembly();
var assemblyVersion = assembly is null
? "1.2.3.4"
: assembly.GetName().Version.ToString();
AssertLayoutRendererOutput("${assembly-version:default=1.2.3.4}", assemblyVersion);
}

[Fact]
public void AssemblyNameVersionTest()
{
AssertLayoutRendererOutput("${assembly-version:NLogAutoLoadExtension}", "2.0.0.0");
}

[Fact]
public void AssemblyNameUnknownVersionTest()
{
using (new NoThrowNLogExceptions())
AssertLayoutRendererOutput("${assembly-version:FooBar:default=1.2.3.4}", "1.2.3.4");
}


[Fact]
public void AssemblyNameVersionTypeTest()
{
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.