-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathEventData.cs
518 lines (452 loc) · 15.7 KB
/
EventData.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------
using System;
using System.Collections.Generic;
using Microsoft.Diagnostics.EventFlow.Metadata;
using Validation;
namespace Microsoft.Diagnostics.EventFlow
{
//
// Note: this class is not thread-safe. Since it will not be used concurrently, we do not want
// to pay the cost of synchronized access to its members.
//
public class EventData: IDeepCloneable<EventData>
{
private Dictionary<string, object> payload;
private Dictionary<string, List<EventMetadata>> metadata;
public DateTimeOffset Timestamp { get; set; }
public string ProviderName { get; set; }
public LogLevel Level { get; set; }
public long Keywords { get; set; }
public IDictionary<string, object> Payload
{
get
{
if (this.payload == null)
{
this.payload = new Dictionary<string, object>();
}
return this.payload;
}
}
public bool TryGetMetadata(string metadataKind, out IReadOnlyCollection<EventMetadata> metadataOfKind)
{
Requires.NotNull(metadataKind, nameof(metadataKind));
metadataOfKind = null;
if (this.metadata == null)
{
return false;
}
List<EventMetadata> existingMetadata;
if (!this.metadata.TryGetValue(metadataKind, out existingMetadata))
{
return false;
}
else
{
metadataOfKind = existingMetadata;
return true;
}
}
public void SetMetadata(EventMetadata newMetadata)
{
Requires.NotNull(newMetadata, nameof(newMetadata));
string metadataKind = newMetadata.MetadataType;
// CONSIDER: should metadataKind string be interned?
if (this.metadata == null)
{
this.metadata = new Dictionary<string, List<EventMetadata>>();
}
List<EventMetadata> existingEntry;
if (!this.metadata.TryGetValue(metadataKind, out existingEntry))
{
existingEntry = new List<EventMetadata>(1);
this.metadata[metadataKind] = existingEntry;
}
existingEntry.Add(newMetadata);
}
/// <summary>
/// Given the property name, retrieve the value from EventData.
/// If the property name is not any common property of EventData, we will look up the payload.
///
/// There can be a problem if some property in payload has the same name with common property (Timestamp for example).
/// In this case, we can add some more functionality like append the propertyName with @, which means look property in payload.
/// </summary>
/// <param name="propertyName">The propertyName</param>
/// <param name="value">The value of the property. Null if the property doesn't exist</param>
/// <returns>True if find the property. False if the property doesn't exist</returns>
public bool TryGetPropertyValue(string propertyName, out object value)
{
// TODO: Fine tuning this piece of logic. .Net core doesn't support creating delegate. If we use reflection to get the property at run time, performance can be an critical issue in this case.
// However, the current implementation also has too many comparison, which may not be better than caching the PropertyInfo() and call GetValue()
value = null;
try
{
if (propertyName.Equals(nameof(Timestamp), StringComparison.OrdinalIgnoreCase))
{
value = this.Timestamp;
}
else if (propertyName.Equals(nameof(ProviderName), StringComparison.OrdinalIgnoreCase))
{
value = this.ProviderName;
}
else if (propertyName.Equals(nameof(Level), StringComparison.OrdinalIgnoreCase))
{
value = this.Level;
}
else if (propertyName.Equals(nameof(Keywords), StringComparison.OrdinalIgnoreCase))
{
value = this.Keywords;
}
else if (Payload.TryGetValue(propertyName, out value))
{
}
else
{
return false;
}
}
catch
{
return false;
}
return true;
}
public delegate void ProcessPayload<T>(T value);
public bool GetValueFromPayload<T>(string payloadName, ProcessPayload<T> handler)
{
object pv = TryGetNonNullPayloadValue(payloadName);
if (pv == null)
{
return false;
}
bool converted = false;
T value = default(T);
try
{
value = (T)pv;
converted = true;
}
catch { }
if (!converted)
{
try
{
value = (T)Convert.ChangeType(pv, typeof(T));
converted = true;
}
catch { }
}
if (converted)
{
handler(value);
}
return converted;
}
// The follosing overloads of GetValueFromPayload eliminate a (handled) InvalidCastException
// when dealing with a few well-known and often-used primitive type combinations.
// See https://github.com/Azure/diagnostics-eventflow/issues/371 for more information.
public bool GetValueFromPayload(string payloadName, ProcessPayload<bool> handler)
{
return GetValueFromPayloadMatchingType<bool>(payloadName, handler) ? true : GetValueFromPayload<bool>(payloadName, handler);
}
public bool GetValueFromPayload(string payloadName, ProcessPayload<DateTime> handler)
{
return GetValueFromPayloadMatchingType<DateTime>(payloadName, handler) ? true : GetValueFromPayload<DateTime>(payloadName, handler);
}
public bool GetValueFromPayload(string payloadName, ProcessPayload<DateTimeOffset> handler)
{
return GetValueFromPayloadMatchingType<DateTimeOffset>(payloadName, handler) ? true : GetValueFromPayload<DateTimeOffset>(payloadName, handler);
}
public bool GetValueFromPayload(string payloadName, ProcessPayload<Guid> handler)
{
return GetValueFromPayloadMatchingType<Guid>(payloadName, handler) ? true : GetValueFromPayload<Guid>(payloadName, handler);
}
public bool GetValueFromPayload(string payloadName, ProcessPayload<TimeSpan> handler)
{
return GetValueFromPayloadMatchingType<TimeSpan>(payloadName, handler) ? true : GetValueFromPayload<TimeSpan>(payloadName, handler);
}
public bool GetValueFromPayload(string payloadName, ProcessPayload<float> handler)
{
object pv = TryGetNonNullPayloadValue(payloadName);
if (pv == null)
{
return false;
}
float? value = null;
switch(pv)
{
case float fv:
value = fv;
break;
case byte bv:
value = bv;
break;
case short sv:
value = sv;
break;
case ushort usv:
value = usv;
break;
case int iv:
value = iv;
break;
case uint uiv:
value = uiv;
break;
case long lv:
value = lv;
break;
case ulong ulv:
value = ulv;
break;
}
if (value.HasValue)
{
handler(value.Value);
return true;
}
else return GetValueFromPayload<float>(payloadName, handler);
}
public bool GetValueFromPayload(string payloadName, ProcessPayload<double> handler)
{
object pv = TryGetNonNullPayloadValue(payloadName);
if (pv == null)
{
return false;
}
double? value = null;
switch (pv)
{
case double dv:
value = dv;
break;
case float fv:
value = fv;
break;
case byte bv:
value = bv;
break;
case short sv:
value = sv;
break;
case ushort usv:
value = usv;
break;
case int iv:
value = iv;
break;
case uint uiv:
value = uiv;
break;
case long lv:
value = lv;
break;
case ulong ulv:
value = ulv;
break;
}
if (value.HasValue)
{
handler(value.Value);
return true;
}
else return GetValueFromPayload<double>(payloadName, handler);
}
public bool GetValueFromPayload(string payloadName, ProcessPayload<int> handler)
{
object pv = TryGetNonNullPayloadValue(payloadName);
if (pv == null)
{
return false;
}
int? value = null;
switch (pv)
{
case int iv:
value = iv;
break;
case byte bv:
value = bv;
break;
case short sv:
value = sv;
break;
case ushort usv:
value = usv;
break;
}
if (value.HasValue)
{
handler(value.Value);
return true;
}
else return GetValueFromPayload<int>(payloadName, handler);
}
public bool GetValueFromPayload(string payloadName, ProcessPayload<long> handler)
{
object pv = TryGetNonNullPayloadValue(payloadName);
if (pv == null)
{
return false;
}
long? value = null;
switch (pv)
{
case long lv:
value = lv;
break;
case int iv:
value = iv;
break;
case uint uiv:
value = uiv;
break;
case byte bv:
value = bv;
break;
case short sv:
value = sv;
break;
case ushort usv:
value = usv;
break;
}
if (value.HasValue)
{
handler(value.Value);
return true;
}
else return GetValueFromPayload<long>(payloadName, handler);
}
public bool GetValueFromPayload(string payloadName, ProcessPayload<uint> handler)
{
object pv = TryGetNonNullPayloadValue(payloadName);
if (pv == null)
{
return false;
}
uint? value = null;
switch (pv)
{
case uint uiv:
value = uiv;
break;
case byte bv:
value = bv;
break;
case ushort usv:
value = usv;
break;
}
if (value.HasValue)
{
handler(value.Value);
return true;
}
else return GetValueFromPayload<uint>(payloadName, handler);
}
public bool GetValueFromPayload(string payloadName, ProcessPayload<ulong> handler)
{
object pv = TryGetNonNullPayloadValue(payloadName);
if (pv == null)
{
return false;
}
ulong? value = null;
switch (pv)
{
case ulong ulv:
value = ulv;
break;
case uint uiv:
value = uiv;
break;
case byte bv:
value = bv;
break;
case ushort usv:
value = usv;
break;
}
if (value.HasValue)
{
handler(value.Value);
return true;
}
else return GetValueFromPayload<ulong>(payloadName, handler);
}
public bool GetValueFromPayload(string payloadName, ProcessPayload<string> handler)
{
object pv = TryGetNonNullPayloadValue(payloadName);
if (pv == null)
{
return false;
}
if (pv is string sv)
{
handler(sv);
return true;
}
try
{
sv = Convert.ToString(pv);
handler(sv);
return true;
}
catch
{
return false;
}
}
public void AddPayloadProperty(string key, object value, IHealthReporter healthReporter, string context)
{
PayloadDictionaryUtilities.AddPayloadProperty(this.Payload, key, value, healthReporter, context);
}
public EventData DeepClone()
{
var other = new EventData();
other.Keywords = this.Keywords;
other.Level = this.Level;
other.ProviderName = this.ProviderName;
other.Timestamp = this.Timestamp;
if (this.payload != null)
{
other.payload = new Dictionary<string, object>(this.payload);
}
if (this.metadata != null)
{
other.metadata = new Dictionary<string, List<EventMetadata>>(this.metadata);
}
return other;
}
private bool GetValueFromPayloadMatchingType<T>(string payloadName, ProcessPayload<T> handler)
{
object pv = TryGetNonNullPayloadValue(payloadName);
if (pv == null)
{
return false;
}
if (pv is T tValue)
{
handler(tValue);
return true;
}
else return false;
}
private object TryGetNonNullPayloadValue(string payloadName)
{
if (string.IsNullOrEmpty(payloadName))
{
return null;
}
if (!Payload.TryGetValue(payloadName, out object pv))
{
return null;
}
return pv;
}
}
}