Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 1 addition & 1 deletion src/Sentry/Internal/DefaultSentryStructuredLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private protected override void CaptureLog(SentryLogLevel level, string template
}

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

CaptureLog(log);
}
Expand Down
18 changes: 17 additions & 1 deletion src/Sentry/SentryLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ internal void SetAttribute(string key, int value)
_attributes[key] = new SentryAttribute(value, "integer");
}

internal void SetDefaultAttributes(SentryOptions options, SdkVersion sdk)
internal void SetDefaultAttributes(SentryOptions options, SdkVersion sdk, Scope? scope = null)
Comment thread
jamescrosswell marked this conversation as resolved.
Outdated
{
var environment = options.SettingLocator.GetEnvironment();
SetAttribute("sentry.environment", environment);
Expand All @@ -191,6 +191,22 @@ internal void SetDefaultAttributes(SentryOptions options, SdkVersion sdk)
{
SetAttribute("sentry.sdk.version", version);
}

if (scope?.User is { } user)
Comment thread
jamescrosswell marked this conversation as resolved.
{
if (user.Id is { } userId)
{
SetAttribute("user.id", userId);
}
if (user.Username is { } username)
{
SetAttribute("user.name", username);
}
if (user.Email is { } email)
{
SetAttribute("user.email", email);
}
}
}

internal void SetOrigin(string origin)
Expand Down
52 changes: 52 additions & 0 deletions test/Sentry.Tests/SentryStructuredLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,58 @@ public void Dispose_BeforeLog_DoesNotCaptureEnvelope()
entry.Args.Should().BeEquivalentTo([nameof(SentryLog)]);
}

[Fact]
public void Log_WithScopeUser_SetsUserAttributes()
{
var scope = new Scope();
scope.User = new SentryUser { Id = "user-id", Username = "user-name", Email = "user@example.com" };
_fixture.Hub.SubstituteConfigureScope(scope);

SentryLog capturedLog = null!;
_fixture.Options.EnableLogs = true;
_fixture.Options.SetBeforeSendLog((SentryLog log) =>
{
capturedLog = log;
return log;
});
var logger = _fixture.GetSut();

logger.LogInfo("A message");
logger.Flush();

capturedLog.Should().NotBeNull();
capturedLog.TryGetAttribute("user.id", out string? userId).Should().BeTrue();
userId.Should().Be("user-id");
capturedLog.TryGetAttribute("user.name", out string? userName).Should().BeTrue();
userName.Should().Be("user-name");
capturedLog.TryGetAttribute("user.email", out string? userEmail).Should().BeTrue();
userEmail.Should().Be("user@example.com");
}

[Fact]
public void Log_WithoutScopeUser_DoesNotSetUserAttributes()
{
var scope = new Scope();
_fixture.Hub.SubstituteConfigureScope(scope);

SentryLog capturedLog = null!;
_fixture.Options.EnableLogs = true;
_fixture.Options.SetBeforeSendLog((SentryLog log) =>
{
capturedLog = log;
return log;
});
var logger = _fixture.GetSut();

logger.LogInfo("A message");
logger.Flush();

capturedLog.Should().NotBeNull();
capturedLog.TryGetAttribute("user.id", out object? _).Should().BeFalse();
capturedLog.TryGetAttribute("user.name", out object? _).Should().BeFalse();
capturedLog.TryGetAttribute("user.email", out object? _).Should().BeFalse();
}

private static void ConfigureLog(SentryLog log)
{
log.SetAttribute("attribute-key", "attribute-value");
Expand Down
Loading