Repository navigation
Expand file tree
/
Copy pathEvent.cs
More file actions
208 lines (183 loc) · 7.52 KB
/
Copy pathEvent.cs
File metadata and controls
208 lines (183 loc) · 7.52 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
using Exceptionless.Core.Extensions;
using Exceptionless.Core.Serialization;
using MiniValidation;
namespace Exceptionless.Core.Models;
[DebuggerDisplay("Type: {Type}, Date: {Date}, Message: {Message}, Value: {Value}, Count: {Count}")]
public class Event : IData, IJsonOnDeserialized
{
private string? _environment;
/// <summary>
/// The deployment environment, such as production, staging, or development.
/// Missing or invalid names remain unspecified. Machine and runtime information is stored separately in data.@environment.
/// </summary>
[StringLength(64)]
public string? Environment
{
get => _environment;
set => _environment = NormalizeEnvironment(value);
}
/// <summary>
/// Returns the trimmed deployment name, or null when the value cannot be a deployment name.
/// </summary>
public static string? NormalizeEnvironment(string? value)
{
string? name = value?.Trim();
return String.IsNullOrEmpty(name) || name.Length > 64 || name.Any(Char.IsControl)
? null
: name;
}
/// <summary>
/// The event type (ie. error, log message, feature usage). Check <see cref="KnownTypes">Event.KnownTypes</see> for standard event types.
/// Nullable in transit; the pipeline infers a default before save. Validated as required on repository save.
/// </summary>
[Required]
[StringLength(100, MinimumLength = 1)]
public string? Type { get; set; }
/// <summary>
/// The event source (ie. machine name, log name, feature name).
/// </summary>
[StringLength(2000, MinimumLength = 1)]
public string? Source { get; set; }
/// <summary>
/// The date that the event occurred on.
/// </summary>
public DateTimeOffset Date { get; set; }
/// <summary>
/// A list of tags used to categorize this event.
/// </summary>
public TagSet? Tags { get; set; } = [];
/// <summary>
/// The event message.
/// </summary>
[StringLength(2000, MinimumLength = 1)]
public string? Message { get; set; }
/// <summary>
/// The geo coordinates where the event happened.
/// </summary>
public string? Geo { get; set; }
/// <summary>
/// The value of the event if any.
/// </summary>
public decimal? Value { get; set; }
/// <summary>
/// The number of duplicated events.
/// </summary>
public int? Count { get; set; }
/// <summary>
/// Optional data entries that contain additional information about this event.
/// </summary>
[SkipRecursion]
public DataDictionary? Data { get; set; } = new();
/// <summary>
/// Captures unknown JSON properties during deserialization.
/// These are merged into <see cref="Data"/> after deserialization.
/// Known data keys like "@error", "@request", "@environment" may appear at root level.
/// </summary>
[JsonExtensionData]
[JsonInclude]
internal Dictionary<string, JsonElement>? ExtensionData { get; set; }
/// <summary>
/// An optional identifier to be used for referencing this event instance at a later time.
/// </summary>
public string? ReferenceId { get; set; }
/// <summary>
/// Called after JSON deserialization to merge extension data into the Data dictionary.
/// This handles the case where known data keys like "@error", "@request", "@environment"
/// appear at the JSON root level instead of nested under "data".
/// </summary>
void IJsonOnDeserialized.OnDeserialized()
{
EventDataNormalizer.Normalize(Data);
if (ExtensionData is { Count: > 0 })
{
// STJ with SnakeCaseLower policy only matches case-insensitively against the
// policy-transformed name "reference_id". PascalCase "ReferenceId" and camelCase
// "referenceId" are entirely different strings (not just different casing) so they
// go to ExtensionData. Required for older .NET SDK versions (< 5.x) that submit
// events with PascalCase/camelCase property names.
if (ReferenceId is null &&
(ExtensionData.Remove("ReferenceId", out var refIdElement) ||
ExtensionData.Remove("referenceId", out refIdElement)))
{
ReferenceId = refIdElement.GetString();
}
Data ??= [];
foreach (var kvp in ExtensionData)
{
object? value = JsonElementConverter.Convert(kvp.Value);
EventDataNormalizer.Set(Data, kvp.Key, value);
}
ExtensionData = null;
}
}
protected bool Equals(Event other)
{
return String.Equals(Environment, other.Environment) && String.Equals(Type, other.Type) && String.Equals(Source, other.Source) && Tags.CollectionEquals(other.Tags) && String.Equals(Message, other.Message) && String.Equals(Geo, other.Geo) && Value == other.Value && Equals(Data, other.Data);
}
public override bool Equals(object? obj)
{
if (obj is null)
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != GetType())
return false;
return Equals((Event)obj);
}
private static readonly List<string> _exclusions = [KnownDataKeys.TraceLog];
public override int GetHashCode()
{
unchecked
{
int hashCode = Type?.GetHashCode() ?? 0;
hashCode = (hashCode * 397) ^ (Source?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ (Tags?.GetCollectionHashCode() ?? 0);
hashCode = (hashCode * 397) ^ (Message?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ (Geo?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ Value.GetHashCode();
hashCode = (hashCode * 397) ^ (Data?.GetCollectionHashCode(_exclusions) ?? 0);
if (Environment is not null)
{
hashCode = (hashCode * 397) ^ Environment.GetHashCode();
}
return hashCode;
}
}
public static class KnownTypes
{
public const string Error = "error";
public const string FeatureUsage = "usage";
public const string Log = "log";
public const string NotFound = "404";
public const string Session = "session";
public const string SessionEnd = "sessionend";
public const string SessionHeartbeat = "heartbeat";
}
public static class KnownTags
{
public const string Critical = "Critical";
public const string Internal = "Internal";
}
public static class KnownDataKeys
{
public const string Error = "@error";
public const string SimpleError = "@simple_error";
public const string RequestInfo = "@request";
public const string TraceLog = "@trace";
public const string EnvironmentInfo = "@environment";
public const string UserInfo = "@user";
public const string UserDescription = "@user_description";
public const string Version = "@version";
public const string Level = "@level";
public const string Location = "@location";
public const string SubmissionMethod = "@submission_method";
public const string SubmissionClient = "@submission_client";
public const string SessionEnd = "sessionend";
public const string SessionHasError = "haserror";
public const string ManualStackingInfo = "@stack";
}
}