-
-
Notifications
You must be signed in to change notification settings - Fork 226
feat: add Serilog integration
#4462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0abf66c
7c73bf1
cca7f57
922185b
962e435
9cec5f8
92a3afd
cfe1703
b0ec5a3
bf68d35
998c105
b3374d1
108caed
4f84ca7
ed13b35
972efc4
02defb5
83013af
7f886cc
d5cf509
c29c704
1a4b3c7
5136730
91f5013
cdf0318
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| 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) | ||
| { | ||
| GetTraceIdAndSpanId(hub, 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, | ||
| ParentSpanId = spanId, | ||
| }; | ||
|
|
||
| log.SetDefaultAttributes(options, Sdk); | ||
|
|
||
| foreach (var attribute in attributes) | ||
| { | ||
| log.SetAttribute(attribute.Key, attribute.Value); | ||
| } | ||
|
|
||
| hub.Logger.CaptureLog(log); | ||
| } | ||
|
|
||
| private static void GetTraceIdAndSpanId(IHub hub, out SentryId traceId, out SpanId? spanId) | ||
| { | ||
| var span = hub.GetSpan(); | ||
| if (span is not null) | ||
| { | ||
| traceId = span.TraceId; | ||
| spanId = span.SpanId; | ||
| return; | ||
| } | ||
|
|
||
| var scope = hub.GetScope(); | ||
| if (scope is not null) | ||
| { | ||
| traceId = scope.PropagationContext.TraceId; | ||
| spanId = scope.PropagationContext.SpanId; | ||
| return; | ||
| } | ||
|
|
||
| traceId = SentryId.Empty; | ||
| spanId = null; | ||
| } | ||
|
|
||
| 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()); | ||
| } | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Inconsistent Structure Handling in LoggingThe |
||
| else if (!property.Value.IsNull()) | ||
| { | ||
| yield return new KeyValuePair<string, object>(property.Key, property.Value); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.