Created
April 24, 2017 20:12
-
-
Save GeorgeTsiokos/ec0527ec3bae0880f9f5233235d7b9dd to your computer and use it in GitHub Desktop.
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
[PublicAPI] | |
public sealed class AssemblyVersionTelemetryInitializer : ITelemetryInitializer | |
{ | |
[CanBeNull] string _version; | |
public void Initialize([CanBeNull] ITelemetry telemetry) | |
{ | |
var component = telemetry?.Context?.Component; | |
if (component == null) | |
return; | |
var version = LazyInitializer.EnsureInitialized(ref _version, GetVersion); | |
component.Version = version; | |
} | |
[CanBeNull] | |
static string GetAssemblyFileVersion([NotNull] Assembly assembly) | |
{ | |
var assemblyFileVersionAttributes = assembly.GetCustomAttributes<AssemblyFileVersionAttribute>(); | |
return assemblyFileVersionAttributes?.FirstOrDefault()?.Version; | |
} | |
[CanBeNull] | |
static string GetAssemblyInformationalVersion([NotNull] Assembly assembly) | |
{ | |
var assemblyInformationalVersionAttributes = assembly.GetCustomAttributes<AssemblyInformationalVersionAttribute>(); | |
return assemblyInformationalVersionAttributes?.FirstOrDefault()?.InformationalVersion; | |
} | |
[NotNull] | |
static string GetAssemblyVersion([NotNull] Assembly assembly) | |
{ | |
return assembly.GetName().Version.ToString(); | |
} | |
[NotNull] | |
static string GetVersion() | |
{ | |
var assembly = Assembly.GetEntryAssembly(); | |
return GetAssemblyInformationalVersion(assembly) ?? GetAssemblyFileVersion(assembly) ?? GetAssemblyVersion(assembly); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment