Skip to content

WIP: Logs for Sentry.Extensions.Logging and integrations for Sentry.AspNetCore and Sentry.Maui #4193

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

Draft
wants to merge 109 commits into
base: feat/logs
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
0add668
feat(logs): initial experiment
Flash0ver Apr 30, 2025
db7d558
feat(logs): basic Logger Module API shape
Flash0ver Apr 30, 2025
d96b092
style(logs): consolidate
Flash0ver May 5, 2025
2958a47
ref(logs): remove generic WriteSerializable overload
Flash0ver May 5, 2025
a63371b
ref(logs): consolidate experimental Diagnostic-ID
Flash0ver May 5, 2025
d8d2567
feat(logs): add experimental options
Flash0ver May 7, 2025
fefd24c
Merge branch 'main' into feat/logs-initial-api
Flash0ver May 7, 2025
165996a
ref(logs): remove custom polyfills now provided through Polyfill
Flash0ver May 7, 2025
32e7e25
Format code
getsentry-bot May 7, 2025
fc88722
Merge branch 'feat/logs-initial-api' of https://github.com/getsentry/…
Flash0ver May 7, 2025
2ba87e4
ref(logs): move types out of Experimental namespace
Flash0ver May 7, 2025
0f1d4a4
feat(logs): change 'integer' from Int32 to Int64
Flash0ver May 7, 2025
8dec5d5
ref(logs): refine API surface area
Flash0ver May 8, 2025
a664f7e
ref(logs): match SeverityLevel to OTel spec
Flash0ver May 8, 2025
96693d0
ref(logs): rename SentrySeverity to LogSeverityLevel
Flash0ver May 8, 2025
83964cf
Format code
getsentry-bot May 8, 2025
dadc69b
ref(logs): hide underlying Dictionary`2 for Attributes
Flash0ver May 9, 2025
c91cdde
ref(logs): restructure attributes
Flash0ver May 9, 2025
0740c3b
ref(logs): extract TraceId and ParentSpanId methods
Flash0ver May 9, 2025
eee06bf
ref(logs): remove `SentryOptions.LogsSampleRate`
Flash0ver May 12, 2025
8c61d8b
feat(logs): support ISystemClock abstraction
Flash0ver May 12, 2025
80683ae
ref(logs): disambiguate SentryLogger names
Flash0ver May 12, 2025
cb20118
ref(logs): consolidate names of Log-Methods
Flash0ver May 12, 2025
2cb306f
ref(logs): rename LogSeverityLevel to SentryLogLevel
Flash0ver May 12, 2025
dcc0ec1
ref(logs): re-rename new logger type
Flash0ver May 13, 2025
58dce74
ref(logs): move Logger instances to Hubs
Flash0ver May 14, 2025
f2e1ba2
test(logs): add tests
Flash0ver May 14, 2025
0220015
Merge branch 'main' into feat/logs-initial-api
Flash0ver May 14, 2025
6822b23
Format code
getsentry-bot May 14, 2025
dd39fae
fix(logs): incorrectly serializing attributes
Flash0ver May 14, 2025
6eb5b9b
fix(logs): do not capture log when template/parameters are invalid
Flash0ver May 14, 2025
69c05b8
fix(logs): do not capture log on user callback exceptions
Flash0ver May 14, 2025
430cf82
ref(logs): move new public types to root namespace
Flash0ver May 14, 2025
31a8f1f
ref(logs): rework sample
Flash0ver May 14, 2025
2ae4476
ref(logs): ensure that DisabledHub dues not capture logs
Flash0ver May 14, 2025
69678ce
ref(logs): allow out-of-range Log-Level
Flash0ver May 14, 2025
97995a8
docs(logs): add XML comments indicating that logs will be ignored on …
Flash0ver May 14, 2025
fbe747d
docs(logs): add to changelog
Flash0ver May 15, 2025
64adf33
fix(logs): add to Bindable-Options
Flash0ver May 15, 2025
cdfa901
fix(logs): add to ApiApprovalTests
Flash0ver May 15, 2025
d2ac53b
test(logs): add missing net48 ApiApproval
Flash0ver May 15, 2025
4011ba6
test(logs): fix line endings on Windows
Flash0ver May 15, 2025
4ae82d0
Update src/Sentry/Protocol/Envelopes/Envelope.cs
Flash0ver May 15, 2025
bc1c465
Update SentryLog.cs
Flash0ver May 15, 2025
79fb190
test(logs): fix floating-point ToString expectation for .NET Framework
Flash0ver May 15, 2025
b4e80f4
ref(logs): remove some using declarations
Flash0ver May 15, 2025
b21adef
test(ci): trying to work around floating-point formatter on .NET Fram…
Flash0ver May 15, 2025
0032858
test(logs): skip failing tests on Mono (non-Windows)
Flash0ver May 15, 2025
a9769f8
test(log): fix Skip.If missing SkippableFact
Flash0ver May 15, 2025
9a51033
try: fix floating-point formatting on Windows
Flash0ver May 15, 2025
8d03449
feat(logs): Sentry.Extensions.Logging
Flash0ver May 16, 2025
1bc3a6f
feat(logs): Sentry.AspNetCore
Flash0ver May 16, 2025
7624baa
feat(logs): Sentry.Maui
Flash0ver May 16, 2025
11fe02b
fix(logs): Logging Filter
Flash0ver May 16, 2025
fd532ca
Format code
getsentry-bot May 16, 2025
1fe9cc0
feat(logs): add CHANGELOG entry
Flash0ver May 16, 2025
476cbc3
Merge branch 'feat/logs-microsoft-extensions-logging' of https://gith…
Flash0ver May 16, 2025
9a09832
Merge branch 'main' into feat/logs-initial-api
Flash0ver May 21, 2025
d9ce523
Merge branch 'main' into feat/logs-microsoft-extensions-logging
Flash0ver May 21, 2025
3e6dba5
release: 5.8.0-alpha.0
getsentry-bot May 21, 2025
23934dc
Merge branch 'release/5.8.0-alpha.0' into feat/logs-microsoft-extensi…
May 22, 2025
f133118
Merge branch 'main' into feat/logs-initial-api
Flash0ver Jun 3, 2025
0985a76
Merge branch 'main' into feat/logs-microsoft-extensions-logging
Flash0ver Jun 3, 2025
72c9a93
ref: make SentryStructuredLogger abstract
Flash0ver Jun 4, 2025
c97f4ad
docs: add comment to sample usage of SetBeforeSendLog
Flash0ver Jun 5, 2025
a9eea90
ref: clarify intent of usages of Debug.Assert
Flash0ver Jun 5, 2025
8bd0ed2
docs: improve XML comments
Flash0ver Jun 5, 2025
51892de
test: range of Severity-Number specification
Flash0ver Jun 5, 2025
acc8995
test: GetValuesAsUnderlyingType of the new enum
Flash0ver Jun 5, 2025
6bd4c96
ref: move Log options to Experimental section
Flash0ver Jun 5, 2025
f673d1e
ref: move Logger to Experimental section of SDK
Flash0ver Jun 5, 2025
daafd7f
test: add Hub tests
Flash0ver Jun 5, 2025
62ee5d5
Merge branch 'feat/logs' into feat/logs-initial-api
Flash0ver Jun 6, 2025
6a54203
test: update verified public API
Flash0ver Jun 6, 2025
479cab8
docs: update CHANGELOG.md
Flash0ver Jun 6, 2025
f59160b
Merge branch 'feat/logs-initial-api' into feat/logs-microsoft-extensi…
Flash0ver Jun 6, 2025
18a8284
ref: move Log-rerelated Options to Experimental sub-section
Flash0ver Jun 6, 2025
97a87f8
Merge branch 'main' into feat/logs-initial-api
jamescrosswell Jun 9, 2025
0467449
Merge branch 'feat/logs' into feat/logs-initial-api
Flash0ver Jun 16, 2025
54062d2
ref: reuse Disabled-Instance when Structured-Logging is not enabled
Flash0ver Jun 16, 2025
7107bce
ref: remove Enabled-checks on Default-Logger
Flash0ver Jun 16, 2025
c0a1cd5
ref: rename DisabledSentryStructuredLogger to NoOpSentryStructuredLogger
Flash0ver Jun 16, 2025
45b8687
ref: make `BindableSentryOptions.Experimental` internal
Flash0ver Jun 16, 2025
3192534
Revert "ref: make `BindableSentryOptions.Experimental` internal"
Flash0ver Jun 16, 2025
b8bcea6
docs: Update CHANGELOG.md
Flash0ver Jun 16, 2025
d4c82a2
Revert "ref: rename DisabledSentryStructuredLogger to NoOpSentryStruc…
Flash0ver Jun 18, 2025
9193a96
ref: replace use of ScopeManager with TraceHeader
Flash0ver Jun 18, 2025
7cb4043
test: remove Skip as we no longer test net48 against non-Windows
Flash0ver Jun 18, 2025
e3ca5b5
feat: support more numeric types
Flash0ver Jun 20, 2025
afb135e
feat: support char attributes
Flash0ver Jun 20, 2025
7f675aa
fix: build error targeting .NET Standard 2.0 and .NET Framework
Flash0ver Jun 20, 2025
750a388
fix: exception when passing null as message parameter
Flash0ver Jun 20, 2025
9f62d3b
test: add Attributes-To-Json test
Flash0ver Jun 20, 2025
5b00c21
fix: missing type on .NET Framework
Flash0ver Jun 24, 2025
6d17918
feat: support Attribute-Types from spec
Flash0ver Jun 24, 2025
7e2c57b
ref: clarify internal identifiers
Flash0ver Jun 24, 2025
7115c42
test: update approved API
Flash0ver Jun 24, 2025
cd5246b
test: fix incorrect expectation
Flash0ver Jun 24, 2025
baf5569
Merge branch 'feat/logs' into feat/logs-initial-api
Flash0ver Jun 24, 2025
e592d03
Merge branch 'feat/logs-initial-api' into feat/logs-microsoft-extensi…
Flash0ver Jun 24, 2025
6e13e95
feat: use "wrapping" SDK's Name and Version
Flash0ver Jun 25, 2025
0a9a3b1
fix: Get-Attribute-API
Flash0ver Jun 25, 2025
934fb36
Merge branch 'feat/logs' into feat/logs-initial-api
Flash0ver Jun 25, 2025
2c1608e
docs: update CHANGELOG
Flash0ver Jun 25, 2025
2b09a79
Merge branch 'feat/logs-initial-api' into feat/logs-microsoft-extensi…
Flash0ver Jun 25, 2025
2950be9
docs: update CHANGELOG
Flash0ver Jun 25, 2025
d428163
Merge branch 'feat/logs' into feat/logs-microsoft-extensions-logging
Flash0ver Jul 11, 2025
a936ec6
test: change options to Experimental section
Flash0ver Jul 11, 2025
0fae148
Merge branch 'feat/logs' into feat/logs-microsoft-extensions-logging
Flash0ver Jul 23, 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
24 changes: 10 additions & 14 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## Unreleased

### Features

- Add experimental support for [Sentry Structured Logging](https://docs.sentry.io/product/explore/logs/) via `SentrySdk.Experimental.Logger` ([#4158](https://github.com/getsentry/sentry-dotnet/pull/4158))
- Add experimental integrations of [Sentry Structured Logging](https://docs.sentry.io/product/explore/logs/) ([#4193](https://github.com/getsentry/sentry-dotnet/pull/4193))
Copy link
Collaborator

Choose a reason for hiding this comment

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

If #4158 and #4193 are both going in the same release, we could probably compress it into a single changelog entry (referencing multiple PRs, as we often do for the dependency bumps)... maybe with some bullet/sub-list items for each of the integrations. It's a bit confusing otherwise having two change log entries announcing structured logging.

- `Sentry.AspNetCore`, enabled via `SentryAspNetCoreOptions.Experimental.EnableLogs`
- `Sentry.Extensions.Logging`, enabled via `SentryLoggingOptions.Experimental.EnableLogs`
- `Sentry.Maui`, enabled via `SentryMauiOptions.Experimental.EnableLogs`

## 5.11.2

### Fixes
Expand All @@ -14,22 +24,8 @@

## 5.11.0

- Moved experimental _Sentry Structured Logger_ from `SentrySdk.Logger` to `SentrySdk.Experimental.Logger` ([#4158](https://github.com/getsentry/sentry-dotnet/pull/4158))
- Moved experimental options for _Sentry Structured Logging_ ([#4193](https://github.com/getsentry/sentry-dotnet/pull/4193))
- `Sentry.AspNetCore`: from `SentryAspNetCoreOptions.EnableLogs` to `SentryAspNetCoreOptions.Experimental.EnableLogs`
- `Sentry.Extensions.Logging`: from `SentryLoggingOptions.EnableLogs` to `SentryLoggingOptions.Experimental.EnableLogs`
- `Sentry.Maui`: from `SentryMauiOptions.EnableLogs` to `SentryMauiOptions.Experimental.EnableLogs`

## 5.8.0-alpha.0

### Features

- Add experimental support for _Sentry Structured Logging_ via `SentrySdk.Logger` ([#4158](https://github.com/getsentry/sentry-dotnet/pull/4158))
- Add experimental integrations of _Sentry Structured Logging_ ([#4193](https://github.com/getsentry/sentry-dotnet/pull/4193))
- `Sentry.AspNetCore`, enabled via `SentryAspNetCoreOptions.EnableLogs`
- `Sentry.Extensions.Logging`, enabled via `SentryLoggingOptions.EnableLogs`
- `Sentry.Maui`, enabled via `SentryMauiOptions.EnableLogs`
- Add experimental support for [_Sentry Structured Logging_](https://docs.sentry.io/product/explore/logs/) via `SentrySdk.Experimental.Logger` ([#4158](https://github.com/getsentry/sentry-dotnet/pull/4158))
- Added non-allocating `ConfigureScope` and `ConfigureScopeAsync` overloads ([#4244](https://github.com/getsentry/sentry-dotnet/pull/4244))
- Add .NET MAUI `AutomationId` element information to breadcrumbs ([#4248](https://github.com/getsentry/sentry-dotnet/pull/4248))
- The HTTP Response Status Code for spans instrumented using OpenTelemetry is now searchable ([#4283](https://github.com/getsentry/sentry-dotnet/pull/4283))
Expand Down
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.Console.Basic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
options.Experimental.SetBeforeSendLog(static log =>
{
// A demonstration of how you can drop logs based on some attribute they have
if (log.TryGetAttribute("suppress", out bool attribute) && attribute)
if (log.TryGetAttribute("suppress", out var attribute) && attribute is true)
{
return null;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Sentry/HubExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,16 @@ internal static ITransactionTracer StartTransaction(
var transaction = hub.GetTransaction();
return transaction?.IsSampled == true ? transaction : null;
}

internal static Scope? GetScope(this IHub hub)
{
if (hub is Hub fullHub)
{
return fullHub.ScopeManager.GetCurrent().Key;
}

Scope? current = null;
hub.ConfigureScope(scope => current = scope);
return current;
}
}
3 changes: 2 additions & 1 deletion src/Sentry/Internal/DefaultSentryStructuredLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ private protected override void CaptureLog(SentryLogLevel level, string template
return;
}

log.SetDefaultAttributes(_options);
var scope = _hub.GetScope();
log.SetDefaultAttributes(_options, scope?.Sdk ?? SdkVersion.Instance);

var configuredLog = log;
if (_options.Experimental.BeforeSendLogInternal is { } beforeSendLog)
Expand Down
12 changes: 7 additions & 5 deletions src/Sentry/SentryLog.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Sentry.Extensibility;
using Sentry.Infrastructure;
using Sentry.Internal;
using Sentry.Protocol;

namespace Sentry;
Expand Down Expand Up @@ -131,7 +130,7 @@ internal SentryLog(DateTimeOffset timestamp, SentryId traceId, SentryLogLevel le
[Experimental(DiagnosticId.ExperimentalFeature)]
public bool TryGetAttribute(string key, [NotNullWhen(true)] out object? value)
{
if (_attributes.TryGetValue(key, out var attribute) && attribute.Type == "object" && attribute.Value is not null)
if (_attributes.TryGetValue(key, out var attribute) && attribute.Value is not null)
{
value = attribute.Value;
return true;
Expand Down Expand Up @@ -168,7 +167,7 @@ internal void SetAttribute(string key, string value)
_attributes[key] = new SentryAttribute(value, "string");
}

internal void SetDefaultAttributes(SentryOptions options)
internal void SetDefaultAttributes(SentryOptions options, SdkVersion sdk)
{
var environment = options.SettingLocator.GetEnvironment();
SetAttribute("sentry.environment", environment);
Expand All @@ -179,8 +178,11 @@ internal void SetDefaultAttributes(SentryOptions options)
SetAttribute("sentry.release", release);
}

SetAttribute("sentry.sdk.name", Constants.SdkName);
if (SdkVersion.Instance.Version is { } version)
if (sdk.Name is { } name)
{
SetAttribute("sentry.sdk.name", name);
}
if (sdk.Version is { } version)
{
SetAttribute("sentry.sdk.version", version);
}
Expand Down
61 changes: 49 additions & 12 deletions test/Sentry.Tests/SentryLogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,51 @@ public SentryLogTests(ITestOutputHelper output)
_output = new TestOutputDiagnosticLogger(output);
}

[Fact]
public void Protocol_Default_VerifyAttributes()
{
var options = new SentryOptions
{
Environment = "my-environment",
Release = "my-release",
};
var sdk = new SdkVersion
{
Name = "Sentry.Test.SDK",
Version = "1.2.3-test+Sentry"
};

var log = new SentryLog(Timestamp, TraceId, (SentryLogLevel)24, "message")
{
Template = "template",
Parameters = ImmutableArray.Create<object>("params"),
ParentSpanId = ParentSpanId,
};
log.SetAttribute("attribute", "value");
log.SetDefaultAttributes(options, sdk);

log.Timestamp.Should().Be(Timestamp);
log.TraceId.Should().Be(TraceId);
log.Level.Should().Be((SentryLogLevel)24);
log.Message.Should().Be("message");
log.Template.Should().Be("template");
log.Parameters.Should().BeEquivalentTo(["params"]);
log.ParentSpanId.Should().Be(ParentSpanId);

log.TryGetAttribute("attribute", out object attribute).Should().BeTrue();
attribute.Should().Be("value");
log.TryGetAttribute("sentry.environment", out string environment).Should().BeTrue();
environment.Should().Be(options.Environment);
log.TryGetAttribute("sentry.release", out string release).Should().BeTrue();
release.Should().Be(options.Release);
log.TryGetAttribute("sentry.sdk.name", out string name).Should().BeTrue();
name.Should().Be(sdk.Name);
log.TryGetAttribute("sentry.sdk.version", out string version).Should().BeTrue();
version.Should().Be(sdk.Version);
log.TryGetAttribute("not-found", out object notFound).Should().BeFalse();
notFound.Should().BeNull();
}

[Fact]
public void WriteTo_Envelope_MinimalSerializedSentryLog()
{
Expand All @@ -31,7 +76,7 @@ public void WriteTo_Envelope_MinimalSerializedSentryLog()
};

var log = new SentryLog(Timestamp, TraceId, SentryLogLevel.Trace, "message");
log.SetDefaultAttributes(options);
log.SetDefaultAttributes(options, new SdkVersion());

var envelope = Envelope.FromLog(log);

Expand Down Expand Up @@ -80,14 +125,6 @@ public void WriteTo_Envelope_MinimalSerializedSentryLog()
"sentry.release": {
"value": "my-release",
"type": "string"
},
"sentry.sdk.name": {
"value": "{{SdkVersion.Instance.Name}}",
"type": "string"
},
"sentry.sdk.version": {
"value": "{{SdkVersion.Instance.Version}}",
"type": "string"
}
}
}
Expand Down Expand Up @@ -117,7 +154,7 @@ public void WriteTo_EnvelopeItem_MaximalSerializedSentryLog()
log.SetAttribute("boolean-attribute", true);
log.SetAttribute("integer-attribute", 3);
log.SetAttribute("double-attribute", 4.4);
log.SetDefaultAttributes(options);
log.SetDefaultAttributes(options, new SdkVersion { Name = "Sentry.Test.SDK", Version = "1.2.3-test+Sentry" });

var envelope = EnvelopeItem.FromLog(log);

Expand Down Expand Up @@ -194,11 +231,11 @@ public void WriteTo_EnvelopeItem_MaximalSerializedSentryLog()
"type": "string"
},
"sentry.sdk.name": {
"value": "{{SdkVersion.Instance.Name}}",
"value": "Sentry.Test.SDK",
"type": "string"
},
"sentry.sdk.version": {
"value": "{{SdkVersion.Instance.Version}}",
"value": "1.2.3-test+Sentry",
"type": "string"
},
"sentry.trace.parent_span_id": {
Expand Down
Loading