-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathFrameworkDescription.NetCore.cs
71 lines (62 loc) · 2.38 KB
/
FrameworkDescription.NetCore.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#if NET
using System.Runtime.InteropServices;
namespace OpenTelemetry.AutoInstrumentation;
internal partial class FrameworkDescription
{
private static FrameworkDescription? _instance;
public static FrameworkDescription Instance
{
get { return _instance ??= Create(); }
}
public static FrameworkDescription Create()
{
var frameworkName = "unknown";
var frameworkVersion = "unknown";
var osPlatform = "unknown";
var osArchitecture = "unknown";
var processArchitecture = "unknown";
try
{
try
{
// RuntimeInformation.FrameworkDescription returns a string like ".NET Framework 4.7.2" or ".NET Core 2.1",
// we want to return everything before the last space
var frameworkDescription = RuntimeInformation.FrameworkDescription;
var index = frameworkDescription.LastIndexOf(' ');
frameworkName = frameworkDescription.Substring(0, index).Trim();
}
catch (Exception e)
{
Log.Error(e, "Error getting framework name from RuntimeInformation");
}
if (RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
{
osPlatform = "Windows";
}
else if (RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
{
osPlatform = "Linux";
}
else if (RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
{
osPlatform = "MacOS";
}
osArchitecture = RuntimeInformation.OSArchitecture.ToString().ToLowerInvariant();
processArchitecture = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
frameworkVersion = Environment.Version.ToString();
}
catch (Exception ex)
{
Log.Error(ex, "Error getting framework description.");
}
return new FrameworkDescription(
name: frameworkName,
productVersion: frameworkVersion,
osPlatform: osPlatform,
osArchitecture: osArchitecture,
processArchitecture: processArchitecture);
}
}
#endif