-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathSentrySink.Structured.cs
More file actions
105 lines (94 loc) · 3.74 KB
/
SentrySink.Structured.cs
File metadata and controls
105 lines (94 loc) · 3.74 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
using Sentry.Internal.Extensions;
using Serilog.Parsing;
namespace Sentry.Serilog;
internal sealed partial class SentrySink
{
private static void CaptureStructuredLog(IHub hub, SentryOptions options, LogEvent logEvent, string formatted, string? template)
{
hub.GetTraceIdAndSpanId(out var traceId, out var spanId);
GetStructuredLoggingParametersAndAttributes(logEvent, out var parameters, out var attributes);
SentryLog log = new(logEvent.Timestamp, traceId, logEvent.Level.ToSentryLogLevel(), formatted)
{
Template = template,
Parameters = parameters,
SpanId = spanId,
};
log.SetDefaultAttributes(options, Sdk);
log.SetOrigin("auto.log.serilog");
foreach (var attribute in attributes)
{
log.SetAttribute(attribute.Key, attribute.Value);
}
hub.Logger.CaptureLog(log);
}
private static void GetStructuredLoggingParametersAndAttributes(LogEvent logEvent, out ImmutableArray<KeyValuePair<string, object>> parameters, out List<KeyValuePair<string, object>> attributes)
{
var propertyNames = new HashSet<string>();
foreach (var token in logEvent.MessageTemplate.Tokens)
{
if (token is PropertyToken property)
{
propertyNames.Add(property.PropertyName);
}
}
var @params = ImmutableArray.CreateBuilder<KeyValuePair<string, object>>();
attributes = new List<KeyValuePair<string, object>>();
foreach (var property in logEvent.Properties)
{
if (propertyNames.Contains(property.Key))
{
foreach (var parameter in GetLogEventProperties(property))
{
@params.Add(parameter);
}
}
else
{
foreach (var attribute in GetLogEventProperties(property))
{
attributes.Add(new KeyValuePair<string, object>($"property.{attribute.Key}", attribute.Value));
}
}
}
parameters = @params.DrainToImmutable();
return;
static IEnumerable<KeyValuePair<string, object>> GetLogEventProperties(KeyValuePair<string, LogEventPropertyValue> property)
{
if (property.Value is ScalarValue scalarValue)
{
if (scalarValue.Value is not null)
{
yield return new KeyValuePair<string, object>(property.Key, scalarValue.Value);
}
}
else if (property.Value is SequenceValue sequenceValue)
{
if (sequenceValue.Elements.Count != 0)
{
yield return new KeyValuePair<string, object>(property.Key, sequenceValue.ToString());
}
}
else if (property.Value is DictionaryValue dictionaryValue)
{
if (dictionaryValue.Elements.Count != 0)
{
yield return new KeyValuePair<string, object>(property.Key, dictionaryValue.ToString());
}
}
else if (property.Value is StructureValue structureValue)
{
foreach (var prop in structureValue.Properties)
{
if (LogEventProperty.IsValidName(prop.Name))
{
yield return new KeyValuePair<string, object>($"{property.Key}.{prop.Name}", prop.Value.ToString());
}
}
}
else if (!property.Value.IsNull())
{
yield return new KeyValuePair<string, object>(property.Key, property.Value);
}
}
}
}