-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
Runtime.cs
124 lines (107 loc) · 3.96 KB
/
Runtime.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
using Sentry.Extensibility;
using Sentry.Internal;
using Sentry.Internal.Extensions;
namespace Sentry.Protocol;
/// <summary>
/// This describes a runtime in more detail.
/// </summary>
/// <remarks>
/// Typically this context is used multiple times if multiple runtimes are involved (for instance if you have a JavaScript application running on top of JVM)
/// </remarks>
/// <seealso href="https://develop.sentry.dev/sdk/event-payloads/contexts/"/>
public sealed class Runtime : IJsonSerializable, ICloneable<Runtime>, IUpdatable<Runtime>
{
/// <summary>
/// Tells Sentry which type of context this is.
/// </summary>
public const string Type = "runtime";
/// <summary>
/// The name of the runtime.
/// </summary>
public string? Name { get; set; }
/// <summary>
/// The version identifier of the runtime.
/// </summary>
public string? Version { get; set; }
/// <summary>
/// An optional raw description that Sentry can use in an attempt to normalize Runtime info.
/// </summary>
/// <remarks>
/// When the system doesn't expose a clear API for <see cref="Name"/> and <see cref="Version"/>
/// this field can be used to provide a raw system info (e.g: .NET Framework 4.7.1).
/// </remarks>
public string? RawDescription { get; set; }
/// <summary>
/// An optional .NET Runtime Identifier string.
/// </summary>
public string? Identifier { get; set; }
/// <summary>
/// An optional build number.
/// </summary>
/// <see href="https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed"/>
public string? Build { get; set; }
/// <summary>
/// Clones this instance
/// </summary>
internal Runtime Clone() => ((ICloneable<Runtime>)this).Clone();
Runtime ICloneable<Runtime>.Clone()
=> new()
{
Name = Name,
Version = Version,
Identifier = Identifier,
Build = Build,
RawDescription = RawDescription
};
/// <summary>
/// Updates this instance with data from the properties in the <paramref name="source"/>,
/// unless there is already a value in the existing property.
/// </summary>
internal void UpdateFrom(Runtime source) => ((IUpdatable<Runtime>)this).UpdateFrom(source);
void IUpdatable.UpdateFrom(object source)
{
if (source is Runtime runtime)
{
((IUpdatable<Runtime>)this).UpdateFrom(runtime);
}
}
void IUpdatable<Runtime>.UpdateFrom(Runtime source)
{
Name ??= source.Name;
Version ??= source.Version;
Identifier ??= source.Identifier;
Build ??= source.Build;
RawDescription ??= source.RawDescription;
}
/// <inheritdoc />
public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? _)
{
writer.WriteStartObject();
writer.WriteString("type", Type);
writer.WriteStringIfNotWhiteSpace("name", Name);
writer.WriteStringIfNotWhiteSpace("version", Version);
writer.WriteStringIfNotWhiteSpace("raw_description", RawDescription);
writer.WriteStringIfNotWhiteSpace("identifier", Identifier);
writer.WriteStringIfNotWhiteSpace("build", Build);
writer.WriteEndObject();
}
/// <summary>
/// Parses from JSON.
/// </summary>
public static Runtime FromJson(JsonElement json)
{
var name = json.GetPropertyOrNull("name")?.GetString();
var version = json.GetPropertyOrNull("version")?.GetString();
var rawDescription = json.GetPropertyOrNull("raw_description")?.GetString();
var identifier = json.GetPropertyOrNull("identifier")?.GetString();
var build = json.GetPropertyOrNull("build")?.GetString();
return new Runtime
{
Name = name,
Version = version,
RawDescription = rawDescription,
Identifier = identifier,
Build = build
};
}
}