-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
RuntimeInfo.cs
163 lines (147 loc) · 6.45 KB
/
RuntimeInfo.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
namespace Sentry.PlatformAbstractions;
// https://github.com/dotnet/corefx/issues/17452
internal static class RuntimeInfo
{
private static readonly Regex RuntimeParseRegex = new(
@"^(?<name>(?:[A-Za-z.]\S*\s?)*)(?:\s|^|$)(?<version>\d\S*)?",
RegexOptions.Compiled | RegexOptions.CultureInvariant);
/// <summary>
/// Gets the current runtime.
/// </summary>
/// <returns>A new instance for the current runtime</returns>
internal static SentryRuntime GetRuntime()
{
var runtime = GetFromRuntimeInformation();
runtime ??= GetFromMonoRuntime();
runtime ??= GetFromEnvironmentVariable();
return runtime.WithAdditionalProperties();
}
internal static SentryRuntime WithAdditionalProperties(this SentryRuntime runtime)
{
#if NETFRAMEWORK
GetNetFxInstallationAndVersion(runtime, out var inst, out var ver);
var version = runtime.Version ?? ver;
var installation = runtime.FrameworkInstallation ?? inst;
return new SentryRuntime(runtime.Name, version, installation, runtime.Raw);
#elif NET5_0_OR_GREATER
var version = runtime.Version ?? GetNetCoreVersion(runtime);
var identifier = runtime.Identifier ?? GetRuntimeIdentifier(runtime);
return new SentryRuntime(runtime.Name, version, runtime.Raw, identifier);
#else
var version = runtime.Version ?? GetNetCoreVersion(runtime);
return new SentryRuntime(runtime.Name, version, runtime.Raw);
#endif
}
internal static SentryRuntime? Parse(string? rawRuntimeDescription, string? name = null)
{
if (rawRuntimeDescription == null)
{
return name == null ? null : new SentryRuntime(name);
}
var match = RuntimeParseRegex.Match(rawRuntimeDescription);
if (match.Success)
{
return new SentryRuntime(
name ?? (match.Groups["name"].Value == string.Empty ? null : match.Groups["name"].Value.Trim()),
match.Groups["version"].Value == string.Empty ? null : match.Groups["version"].Value.Trim(),
raw: rawRuntimeDescription);
}
return new SentryRuntime(name, raw: rawRuntimeDescription);
}
#if NETFRAMEWORK
internal static void GetNetFxInstallationAndVersion(
SentryRuntime runtime,
out FrameworkInstallation? frameworkInstallation,
out string? version)
{
if (runtime.IsNetFx() != true)
{
frameworkInstallation = null;
version = null;
return;
}
frameworkInstallation = FrameworkInfo.GetLatest(Environment.Version.Major);
if (frameworkInstallation?.Version?.Major < 4)
{
// prior to 4, user-friendly versions are always 2 digit: 1.0, 1.1, 2.0, 3.0, 3.5
version = frameworkInstallation.ServicePack == null
? $"{frameworkInstallation.Version.Major}.{frameworkInstallation.Version.Minor}"
: $"{frameworkInstallation.Version.Major}.{frameworkInstallation.Version.Minor} SP {frameworkInstallation.ServicePack}";
}
else
{
version = frameworkInstallation?.Version?.ToString();
}
}
#else
private static string? GetNetCoreVersion(SentryRuntime runtime)
{
var description = RuntimeInformation.FrameworkDescription;
return RemovePrefixOrNull(description, ".NET Core")
?? RemovePrefixOrNull(description, ".NET Framework")
?? RemovePrefixOrNull(description, ".NET Native")
?? RemovePrefixOrNull(description, ".NET");
static string? RemovePrefixOrNull(string? value, string prefix)
=> value?.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) == true
? value.Substring(prefix.Length)
: null;
}
#endif
#if NET5_0_OR_GREATER
internal static string? GetRuntimeIdentifier(SentryRuntime runtime)
{
try
{
return RuntimeInformation.RuntimeIdentifier;
}
catch
{
return null;
}
}
#endif
private static SentryRuntime? GetFromRuntimeInformation()
{
try
{
// Preferred API: netstandard2.0
// https://github.com/dotnet/corefx/blob/master/src/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs
// https://github.com/mono/mono/blob/90b49aa3aebb594e0409341f9dca63b74f9df52e/mcs/class/corlib/System.Runtime.InteropServices.RuntimeInformation/RuntimeInformation.cs
// e.g: .NET Framework 4.7.2633.0, .NET Native, WebAssembly
// Note: this throws on some Unity IL2CPP versions
var frameworkDescription = RuntimeInformation.FrameworkDescription;
return Parse(frameworkDescription);
}
catch
{
return null;
}
}
private static SentryRuntime? GetFromMonoRuntime()
=> Type.GetType("Mono.Runtime", false)
?.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static)
?.Invoke(null, null) is string monoVersion
// The implementation of Mono to RuntimeInformation:
// https://github.com/mono/mono/blob/90b49aa3aebb594e0409341f9dca63b74f9df52e/mcs/class/corlib/System.Runtime.InteropServices.RuntimeInformation/RuntimeInformation.cs#L40
// Examples:
// Mono 5.10.1.47 (2017-12/8eb8f7d5e74 Fri Apr 13 20:18:12 EDT 2018)
// Mono 5.10.1.47 (tarball Tue Apr 17 09:23:16 UTC 2018)
// Mono 5.10.0 (Visual Studio built mono)
? Parse(monoVersion, "Mono")
: null;
// This should really only be used on .NET 1.0, 1.1, 2.0, 3.0, 3.5 and 4.0
private static SentryRuntime GetFromEnvironmentVariable()
{
// Environment.Version: NET Framework 4, 4.5, 4.5.1, 4.5.2 = 4.0.30319.xxxxx
// .NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1 = 4.0.30319.42000
// Not recommended on NET45+ (which has RuntimeInformation)
var version = Environment.Version;
var friendlyVersion = version.Major switch
{
1 => "",
_ => version.ToString()
};
return new SentryRuntime(".NET Framework", friendlyVersion, raw: "Environment.Version=" + version);
}
}