-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathFrameworkDescription.NetFramework.cs
138 lines (117 loc) · 4.49 KB
/
FrameworkDescription.NetFramework.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#if NETFRAMEWORK
using System.Reflection;
using Microsoft.Win32;
namespace OpenTelemetry.AutoInstrumentation;
internal partial class FrameworkDescription
{
private static readonly Assembly RootAssembly = typeof(object).Assembly;
private static readonly Tuple<int, string>[] DotNetFrameworkVersionMapping =
{
// known min value for each framework version
Tuple.Create(533320, "4.8.1"),
Tuple.Create(528040, "4.8"),
Tuple.Create(461808, "4.7.2"),
Tuple.Create(461308, "4.7.1"),
Tuple.Create(460798, "4.7"),
Tuple.Create(394802, "4.6.2"),
Tuple.Create(394254, "4.6.1"),
Tuple.Create(393295, "4.6"),
Tuple.Create(379893, "4.5.2"),
Tuple.Create(378675, "4.5.1"),
Tuple.Create(378389, "4.5"),
};
private static FrameworkDescription? _instance;
public static FrameworkDescription Instance
{
get { return _instance ??= Create(); }
}
public static FrameworkDescription Create()
{
var osArchitecture = "unknown";
var processArchitecture = "unknown";
var frameworkVersion = "unknown";
try
{
osArchitecture = Environment.Is64BitOperatingSystem ? "x64" : "x86";
processArchitecture = Environment.Is64BitProcess ? "x64" : "x86";
frameworkVersion = GetNetFrameworkVersion();
}
catch (Exception ex)
{
Log.Error(ex, "Error getting framework description.");
}
return new FrameworkDescription(
name: ".NET Framework",
productVersion: frameworkVersion,
osPlatform: "Windows",
osArchitecture: osArchitecture,
processArchitecture: processArchitecture);
}
private static string GetNetFrameworkVersion()
{
string? productVersion = null;
try
{
object? registryValue;
using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default))
using (var subKey = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"))
{
registryValue = subKey?.GetValue("Release");
}
if (registryValue is int release)
{
// find the known version on the list with the largest release number
// that is lower than or equal to the release number in the Windows Registry
productVersion = DotNetFrameworkVersionMapping.FirstOrDefault(t => release >= t.Item1)?.Item2;
}
}
catch (Exception e)
{
Log.Error(e, "Error getting .NET Framework version from Windows Registry");
}
if (productVersion == null)
{
// if we fail to extract version from assembly path,
// fall back to the [AssemblyInformationalVersion] or [AssemblyFileVersion]
productVersion = GetVersionFromAssemblyAttributes();
}
if (productVersion == null)
{
// at this point, everything else has failed (this is probably the same as [AssemblyFileVersion] above)
productVersion = Environment.Version.ToString();
}
return productVersion;
}
private static string? GetVersionFromAssemblyAttributes()
{
string? productVersion = null;
try
{
// if we fail to extract version from assembly path, fall back to the [AssemblyInformationalVersion],
var informationalVersionAttribute = RootAssembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
// split remove the commit hash from pre-release versions
productVersion = informationalVersionAttribute?.InformationalVersion?.Split('+')[0];
}
catch (Exception e)
{
Log.Error(e, "Error getting framework version from [AssemblyInformationalVersion]");
}
if (productVersion == null)
{
try
{
// and if that fails, try [AssemblyFileVersion]
var fileVersionAttribute = RootAssembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
productVersion = fileVersionAttribute?.Version;
}
catch (Exception e)
{
Log.Error(e, "Error getting framework version from [AssemblyFileVersion]");
}
}
return productVersion;
}
}
#endif