Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0abf66c
feat(logs): add Serilog integration
Flash0ver Aug 20, 2025
7c73bf1
test: and fix
Flash0ver Aug 21, 2025
cca7f57
Format code
getsentry-bot Aug 21, 2025
922185b
docs: update CHANGELOG.md
Flash0ver Aug 21, 2025
962e435
test: flush structured logger
Flash0ver Aug 21, 2025
9cec5f8
perf: change lookup from List to HashSet
Flash0ver Aug 22, 2025
92a3afd
Merge branch 'main' into feat/logs-serilog
jamescrosswell Aug 25, 2025
cfe1703
ref(logs): reorder parameters
Flash0ver Aug 27, 2025
b0ec5a3
ref(logs): inline method
Flash0ver Aug 27, 2025
bf68d35
test: update API approvals
Flash0ver Aug 27, 2025
998c105
test(logs): flush client as well
Flash0ver Aug 27, 2025
b3374d1
Merge branch 'main' into feat/logs-serilog
jamescrosswell Sep 5, 2025
108caed
Merge branch 'main' into feat/logs-serilog
Flash0ver Sep 10, 2025
4f84ca7
ref: remove option from overload not initializing the SDK
Flash0ver Sep 10, 2025
ed13b35
fix: Event and Breadcrumbs disabled but Logs enabled
Flash0ver Sep 10, 2025
972efc4
fix: TraceId and ParentSpanId
Flash0ver Sep 10, 2025
02defb5
fix: use Hub-Options over Sink-Options to allow SDK-Init via Serilog …
Flash0ver Sep 10, 2025
83013af
Merge branch 'main' into feat/logs-serilog
Flash0ver Sep 11, 2025
7f886cc
docs: update CHANGELOG after release
Flash0ver Sep 11, 2025
d5cf509
Merge branch 'main' into feat/logs-serilog
Flash0ver Sep 16, 2025
c29c704
docs: update CHANGELOG after merge
Flash0ver Sep 16, 2025
1a4b3c7
Merge branch 'main' into feat/logs-serilog
Flash0ver Sep 24, 2025
5136730
docs: add comment about options
Flash0ver Sep 24, 2025
91f5013
ref: remove redundant null-check
Flash0ver Sep 24, 2025
cdf0318
ref: remove another redundant null-check
Flash0ver Sep 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Add (experimental) _Structured Logs_ integration for `Serilog` ([#4462](https://github.com/getsentry/sentry-dotnet/pull/4462))

### Fixes

- Upload linked PDBs to fix non-IL-stripped symbolication for iOS ([#4527](https://github.com/getsentry/sentry-dotnet/pull/4527))
Expand Down
2 changes: 2 additions & 0 deletions samples/Sentry.Samples.Serilog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ private static void Main()
// Error and higher is sent as event (default is Error)
options.MinimumEventLevel = LogEventLevel.Error;
options.AttachStacktrace = true;
// send structured logs to Sentry
options.Experimental.EnableLogs = true;
// send PII like the username of the user logged in to the device
options.SendDefaultPii = true;
// Optional Serilog text formatter used to format LogEvent to string. If TextFormatter is set, FormatProvider is ignored.
Expand Down
14 changes: 14 additions & 0 deletions src/Sentry.Serilog/LogLevelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,18 @@ public static BreadcrumbLevel ToBreadcrumbLevel(this LogEventLevel level)
_ => (BreadcrumbLevel)level
};
}

public static SentryLogLevel ToSentryLogLevel(this LogEventLevel level)
{
return level switch
{
LogEventLevel.Verbose => SentryLogLevel.Trace,
LogEventLevel.Debug => SentryLogLevel.Debug,
LogEventLevel.Information => SentryLogLevel.Info,
LogEventLevel.Warning => SentryLogLevel.Warning,
LogEventLevel.Error => SentryLogLevel.Error,
LogEventLevel.Fatal => SentryLogLevel.Fatal,
_ => (SentryLogLevel)level,
};
}
}
126 changes: 126 additions & 0 deletions src/Sentry.Serilog/SentrySink.Structured.cs
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());
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Inconsistent Structure Handling in Logging

The GetLogEventProperties method handles StructureValue inconsistently with other complex types like SequenceValue and DictionaryValue. It flattens structure properties into multiple key-value pairs using dot notation, rather than capturing the entire structure as a single string-represented value. This causes incorrect structured logging and test failures.

Fix in Cursor Fix in Web

else if (!property.Value.IsNull())
{
yield return new KeyValuePair<string, object>(property.Key, property.Value);
}
}
}
}
70 changes: 48 additions & 22 deletions src/Sentry.Serilog/SentrySink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ namespace Sentry.Serilog;
/// </summary>
/// <inheritdoc cref="IDisposable" />
/// <inheritdoc cref="ILogEventSink" />
internal sealed class SentrySink : ILogEventSink, IDisposable
internal sealed partial class SentrySink : ILogEventSink, IDisposable
{
private readonly IDisposable? _sdkDisposable;
private readonly SentrySerilogOptions _options;

internal static readonly SdkVersion NameAndVersion
= typeof(SentrySink).Assembly.GetNameAndVersion();

private static readonly SdkVersion Sdk = new()
{
Name = SdkName,
Version = NameAndVersion.Version,
};

/// <summary>
/// Serilog SDK name.
/// </summary>
Expand Down Expand Up @@ -50,6 +56,11 @@ internal SentrySink(

public void Emit(LogEvent logEvent)
{
if (!IsEnabled(logEvent))
{
return;
}

if (isReentrant.Value)
{
_options.DiagnosticLogger?.LogError($"Reentrant log event detected. Logging when inside the scope of another log event can cause a StackOverflowException. LogEventInfo.Message: {logEvent.MessageTemplate.Text}");
Expand All @@ -67,6 +78,15 @@ public void Emit(LogEvent logEvent)
}
}

private bool IsEnabled(LogEvent logEvent)
{
var options = _hubAccessor().GetSentryOptions();

return logEvent.Level >= _options.MinimumEventLevel
|| logEvent.Level >= _options.MinimumBreadcrumbLevel
|| options?.Experimental.EnableLogs is true;
}

private void InnerEmit(LogEvent logEvent)
{
if (logEvent.TryGetSourceContext(out var context))
Expand All @@ -77,8 +97,7 @@ private void InnerEmit(LogEvent logEvent)
}
}

var hub = _hubAccessor();
if (hub is null || !hub.IsEnabled)
if (_hubAccessor() is not { IsEnabled: true } hub)
{
return;
}
Expand Down Expand Up @@ -122,30 +141,37 @@ private void InnerEmit(LogEvent logEvent)
}
}

if (logEvent.Level < _options.MinimumBreadcrumbLevel)
if (logEvent.Level >= _options.MinimumBreadcrumbLevel)
{
return;
Dictionary<string, string>? data = null;
if (exception != null && !string.IsNullOrWhiteSpace(formatted))
{
// Exception.Message won't be used as Breadcrumb message
// Avoid losing it by adding as data:
data = new Dictionary<string, string>
{
{ "exception_message", exception.Message }
};
}

hub.AddBreadcrumb(
_clock,
string.IsNullOrWhiteSpace(formatted)
? exception?.Message ?? ""
: formatted,
context,
data: data,
level: logEvent.Level.ToBreadcrumbLevel());
}

Dictionary<string, string>? data = null;
if (exception != null && !string.IsNullOrWhiteSpace(formatted))
// Read the options from the Hub, rather than the Sink's Serilog-Options, because 'EnableLogs' is declared in the base 'SentryOptions', rather than the derived 'SentrySerilogOptions'.
// In cases where Sentry's Serilog-Sink is added without a DSN (i.e., without initializing the SDK) and the SDK is initialized differently (e.g., through ASP.NET Core),
// then the 'EnableLogs' option of this Sink's Serilog-Options is default, but the Hub's Sentry-Options have the actual user-defined value configured.
var options = hub.GetSentryOptions();
if (options?.Experimental.EnableLogs is true)
{
// Exception.Message won't be used as Breadcrumb message
// Avoid losing it by adding as data:
data = new Dictionary<string, string>
{
{"exception_message", exception.Message}
};
CaptureStructuredLog(hub, options, logEvent, formatted, template);
}

hub.AddBreadcrumb(
_clock,
string.IsNullOrWhiteSpace(formatted)
? exception?.Message ?? ""
: formatted,
context,
data: data,
level: logEvent.Level.ToBreadcrumbLevel());
}

private static bool IsSentryContext(string context) =>
Expand Down
27 changes: 18 additions & 9 deletions src/Sentry.Serilog/SentrySinkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public static class SentrySinkExtensions
/// </summary>
/// <param name="loggerConfiguration">The logger configuration .<seealso cref="LoggerSinkConfiguration"/></param>
/// <param name="dsn">The Sentry DSN (required). <seealso cref="SentryOptions.Dsn"/></param>
/// <param name="minimumEventLevel">Minimum log level to send an event. <seealso cref="SentrySerilogOptions.MinimumEventLevel"/></param>
/// <param name="minimumBreadcrumbLevel">Minimum log level to record a breadcrumb. <seealso cref="SentrySerilogOptions.MinimumBreadcrumbLevel"/></param>
/// <param name="minimumEventLevel">Minimum log level to send an event. <seealso cref="SentrySerilogOptions.MinimumEventLevel"/></param>
/// <param name="formatProvider">The Serilog format provider. <seealso cref="IFormatProvider"/></param>
/// <param name="textFormatter">The Serilog text formatter. <seealso cref="ITextFormatter"/></param>
/// <param name="sendDefaultPii">Whether to include default Personal Identifiable information. <seealso cref="SentryOptions.SendDefaultPii"/></param>
Expand All @@ -35,6 +35,7 @@ public static class SentrySinkExtensions
/// <param name="reportAssembliesMode">What mode to use for reporting referenced assemblies in each event sent to sentry. Defaults to <see cref="Sentry.ReportAssembliesMode.Version"/></param>
/// <param name="deduplicateMode">What modes to use for event automatic de-duplication. <seealso cref="SentryOptions.DeduplicateMode"/></param>
/// <param name="defaultTags">Default tags to add to all events. <seealso cref="SentryOptions.DefaultTags"/></param>
/// <param name="experimentalEnableLogs">Whether to send structured logs. <seealso cref="SentryOptions.SentryExperimentalOptions.EnableLogs"/></param>
/// <returns><see cref="LoggerConfiguration"/></returns>
/// <example>This sample shows how each item may be set from within a configuration file:
/// <code>
Expand All @@ -50,7 +51,7 @@ public static class SentrySinkExtensions
/// "dsn": "https://[email protected]",
/// "minimumBreadcrumbLevel": "Verbose",
/// "minimumEventLevel": "Error",
/// "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}"///
/// "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}",
/// "sendDefaultPii": false,
/// "isEnvironmentUser": false,
/// "serverName": "MyServerName",
Expand All @@ -71,7 +72,8 @@ public static class SentrySinkExtensions
/// "defaultTags": {
/// "key-1", "value-1",
/// "key-2", "value-2"
/// }
/// },
/// "experimentalEnableLogs": true
/// }
/// }
/// ]
Expand Down Expand Up @@ -103,7 +105,8 @@ public static LoggerConfiguration Sentry(
SentryLevel? diagnosticLevel = null,
ReportAssembliesMode? reportAssembliesMode = null,
DeduplicateMode? deduplicateMode = null,
Dictionary<string, string>? defaultTags = null)
Dictionary<string, string>? defaultTags = null,
bool? experimentalEnableLogs = null)
{
return loggerConfiguration.Sentry(o => ConfigureSentrySerilogOptions(o,
dsn,
Expand All @@ -128,7 +131,8 @@ public static LoggerConfiguration Sentry(
diagnosticLevel,
reportAssembliesMode,
deduplicateMode,
defaultTags));
defaultTags,
experimentalEnableLogs));
}

/// <summary>
Expand Down Expand Up @@ -157,7 +161,7 @@ public static LoggerConfiguration Sentry(
/// "Args": {
/// "minimumEventLevel": "Error",
/// "minimumBreadcrumbLevel": "Verbose",
/// "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}"///
/// "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}"
/// }
/// }
/// ]
Expand Down Expand Up @@ -205,7 +209,8 @@ internal static void ConfigureSentrySerilogOptions(
SentryLevel? diagnosticLevel = null,
ReportAssembliesMode? reportAssembliesMode = null,
DeduplicateMode? deduplicateMode = null,
Dictionary<string, string>? defaultTags = null)
Dictionary<string, string>? defaultTags = null,
bool? experimentalEnableLogs = null)
{
if (dsn is not null)
{
Expand Down Expand Up @@ -317,6 +322,11 @@ internal static void ConfigureSentrySerilogOptions(
sentrySerilogOptions.DeduplicateMode = deduplicateMode.Value;
}

if (experimentalEnableLogs.HasValue)
{
sentrySerilogOptions.Experimental.EnableLogs = experimentalEnableLogs.Value;
}

// Serilog-specific items
sentrySerilogOptions.InitializeSdk = dsn is not null; // Inferred from the Sentry overload that is used
if (defaultTags?.Count > 0)
Expand Down Expand Up @@ -354,7 +364,6 @@ public static LoggerConfiguration Sentry(
sdkDisposable = SentrySdk.Init(options);
}

var minimumOverall = (LogEventLevel)Math.Min((int)options.MinimumBreadcrumbLevel, (int)options.MinimumEventLevel);
return loggerConfiguration.Sink(new SentrySink(options, sdkDisposable), minimumOverall);
return loggerConfiguration.Sink(new SentrySink(options, sdkDisposable));
}
}
1 change: 1 addition & 0 deletions src/Sentry/SentryLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Sentry;
/// <para>This API is experimental and it may change in the future.</para>
/// </summary>
[Experimental(DiagnosticId.ExperimentalFeature)]
[DebuggerDisplay(@"SentryLog \{ Level = {Level}, Message = '{Message}' \}")]
public sealed class SentryLog
{
private readonly Dictionary<string, SentryAttribute> _attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace Serilog
Sentry.SentryLevel? diagnosticLevel = default,
Sentry.ReportAssembliesMode? reportAssembliesMode = default,
Sentry.DeduplicateMode? deduplicateMode = default,
System.Collections.Generic.Dictionary<string, string>? defaultTags = null) { }
System.Collections.Generic.Dictionary<string, string>? defaultTags = null,
bool? experimentalEnableLogs = default) { }
}
}
Loading
Loading