Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GeorgeTsiokos/ec0527ec3bae0880f9f5233235d7b9dd to your computer and use it in GitHub Desktop.
Save GeorgeTsiokos/ec0527ec3bae0880f9f5233235d7b9dd to your computer and use it in GitHub Desktop.
[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