-
-
Notifications
You must be signed in to change notification settings - Fork 230
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 5 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,115 @@ | ||
| using Sentry.Internal.Extensions; | ||
| using Serilog.Parsing; | ||
|
|
||
| namespace Sentry.Serilog; | ||
|
|
||
| internal sealed partial class SentrySink | ||
| { | ||
| private static readonly SdkVersion Sdk = CreateSdkVersion(); | ||
|
|
||
| private void CaptureStructuredLog(IHub hub, LogEvent logEvent, string formatted, string? template) | ||
| { | ||
| var traceHeader = hub.GetTraceHeader() ?? SentryTraceHeader.Empty; | ||
| GetStructuredLoggingParametersAndAttributes(logEvent, out var parameters, out var attributes); | ||
|
|
||
| SentryLog log = new(logEvent.Timestamp, traceHeader.TraceId, logEvent.Level.ToSentryLogLevel(), formatted) | ||
| { | ||
| Template = template, | ||
| Parameters = parameters, | ||
| ParentSpanId = traceHeader.SpanId, | ||
| }; | ||
|
|
||
| log.SetDefaultAttributes(_options, Sdk); | ||
|
|
||
| foreach (var attribute in attributes) | ||
| { | ||
| log.SetAttribute(attribute.Key, attribute.Value); | ||
|
seer-by-sentry[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| hub.Logger.CaptureLog(log); | ||
| } | ||
|
|
||
| private static void GetStructuredLoggingParametersAndAttributes(LogEvent logEvent, out ImmutableArray<KeyValuePair<string, object>> parameters, out List<KeyValuePair<string, object>> attributes) | ||
| { | ||
| var propertyTokens = new List<PropertyToken>(); | ||
| foreach (var token in logEvent.MessageTemplate.Tokens) | ||
| { | ||
| if (token is PropertyToken property) | ||
| { | ||
| propertyTokens.Add(property); | ||
| } | ||
| } | ||
|
|
||
|
seer-by-sentry[bot] marked this conversation as resolved.
|
||
| var @params = ImmutableArray.CreateBuilder<KeyValuePair<string, object>>(); | ||
| attributes = new List<KeyValuePair<string, object>>(); | ||
|
|
||
| foreach (var property in logEvent.Properties) | ||
| { | ||
| if (propertyTokens.Exists(prop => prop.PropertyName == 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) | ||
| { | ||
|
seer-by-sentry[bot] marked this conversation as resolved.
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static SdkVersion CreateSdkVersion() | ||
|
jamescrosswell marked this conversation as resolved.
Outdated
|
||
| { | ||
| return new SdkVersion | ||
| { | ||
| Name = SdkName, | ||
| Version = NameAndVersion.Version, | ||
| }; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
ParentSpanIdrequired in the protocol for StructuredLogging? An empty SpanId probably isn't that useful otherwise eh?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No ... it's an optional "Default Attribute" ... the
trace_idis required.Oops ... I made this mistake in the Sentry-SDK and Microsoft.Extensions.Logging integrations, too.
I'll create a follow-up PR to fix this issue in all integrations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
follow-up for both the SDK-Logger and the
Microsoft.Extensions.Loggingintegration: #4565