Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion benchmarks/Sentry.Benchmarks/BatchProcessorBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public void Setup()
SentryOptions options = new()
{
Dsn = DsnSamples.ValidDsn,
EnableLogs = true,
};

var batchInterval = Timeout.InfiniteTimeSpan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public void Setup()
SentryOptions options = new()
{
Dsn = DsnSamples.ValidDsn,
EnableLogs = true,
};
options.SetBeforeSendLog((SentryLog log) =>
{
Expand Down
2 changes: 0 additions & 2 deletions samples/Sentry.Samples.Console.Basic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
// This option tells Sentry to capture 100% of traces. You still need to start transactions and spans.
options.TracesSampleRate = 1.0;

// This option enables Sentry Logs created via SentrySdk.Logger.
options.EnableLogs = true;
options.SetBeforeSendLog(static log =>
{
// A demonstration of how you can drop logs based on some attribute they have
Expand Down
1 change: 0 additions & 1 deletion src/Sentry/IHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public interface IHub : ISentryClient, ISentryScopeManager
/// <remarks>
/// Available options:
/// <list type="bullet">
/// <item><see cref="Sentry.SentryOptions.EnableLogs"/></item>
/// <item><see cref="Sentry.SentryOptions.SetBeforeSendLog(System.Func{SentryLog, SentryLog})"/></item>
/// </list>
/// </remarks>
Expand Down
1 change: 0 additions & 1 deletion src/Sentry/Internal/DefaultSentryStructuredLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ internal sealed class DefaultSentryStructuredLogger : SentryStructuredLogger, ID
internal DefaultSentryStructuredLogger(IHub hub, SentryOptions options, ISystemClock clock, int batchCount, TimeSpan batchInterval)
{
Debug.Assert(hub.IsEnabled);
Debug.Assert(options is { EnableLogs: true });

_hub = hub;
_options = options;
Expand Down
9 changes: 7 additions & 2 deletions src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,14 @@ public void SetBeforeBreadcrumb(Func<Breadcrumb, Breadcrumb?> beforeBreadcrumb)
}

/// <summary>
/// When set to <see langword="true"/>, logs are sent to Sentry.
/// Defaults to <see langword="false"/>.
/// When set to <see langword="true"/>, logs captured by the logging integrations
/// (<c>Sentry.Extensions.Logging</c>, <c>Sentry.Serilog</c>, <c>Sentry.NLog</c>, <c>Sentry.Log4Net</c>)
/// are sent to Sentry. Defaults to <see langword="false"/>.
/// </summary>
/// <remarks>
/// This option does not apply to logs created directly via <see cref="SentryStructuredLogger"/>
/// (typically <c>SentrySdk.Logger</c>), which are always sent.
/// </remarks>
/// <seealso href="https://develop.sentry.dev/sdk/telemetry/logs/"/>
public bool EnableLogs { get; set; } = false;

Expand Down
4 changes: 1 addition & 3 deletions src/Sentry/SentryStructuredLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ internal static SentryStructuredLogger Create(IHub hub, SentryOptions options, I

internal static SentryStructuredLogger Create(IHub hub, SentryOptions options, ISystemClock clock, int batchCount, TimeSpan batchInterval)
{
return options.EnableLogs
? new DefaultSentryStructuredLogger(hub, options, clock, batchCount, batchInterval)
: DisabledSentryStructuredLogger.Instance;
return new DefaultSentryStructuredLogger(hub, options, clock, batchCount, batchInterval);
}

private protected SentryStructuredLogger()
Expand Down
40 changes: 5 additions & 35 deletions test/Sentry.Tests/HubTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1994,9 +1994,10 @@ public async Task CaptureTransaction_WithTransactionProfiler_SendsTransactionWit
}

[Fact]
public void Logger_IsDisabled_DoesNotCaptureLog()
public void Logger_EnableLogsDisabled_StillCapturesLog()
{
// Arrange
// EnableLogs gates the logging integrations. Logs created directly via this API are always captured.
Assert.False(_fixture.Options.EnableLogs);
var hub = _fixture.GetSut();

Expand All @@ -2005,19 +2006,18 @@ public void Logger_IsDisabled_DoesNotCaptureLog()
hub.Logger.Flush();

// Assert
_fixture.Client.Received(0).CaptureEnvelope(
_fixture.Client.Received(1).CaptureEnvelope(
Arg.Is<Envelope>(envelope =>
envelope.Items.Single(item => item.Header["type"].Equals("log")).Payload.GetType().IsAssignableFrom(typeof(JsonSerializable))
)
);
hub.Logger.Should().BeOfType<DisabledSentryStructuredLogger>();
hub.Logger.Should().BeOfType<DefaultSentryStructuredLogger>();
}

[Fact]
public void Logger_IsEnabled_DoesCaptureLog()
public void Logger_DoesCaptureLog()
{
// Arrange
_fixture.Options.EnableLogs = true;
var hub = _fixture.GetSut();

// Act
Expand All @@ -2033,39 +2033,10 @@ public void Logger_IsEnabled_DoesCaptureLog()
hub.Logger.Should().BeOfType<DefaultSentryStructuredLogger>();
}

[Fact]
public void Logger_EnableAfterCreate_HasNoEffect()
{
// Arrange
Assert.False(_fixture.Options.EnableLogs);
var hub = _fixture.GetSut();

// Act
_fixture.Options.EnableLogs = true;

// Assert
hub.Logger.Should().BeOfType<DisabledSentryStructuredLogger>();
}

[Fact]
public void Logger_DisableAfterCreate_HasNoEffect()
{
// Arrange
_fixture.Options.EnableLogs = true;
var hub = _fixture.GetSut();

// Act
_fixture.Options.EnableLogs = false;

// Assert
hub.Logger.Should().BeOfType<DefaultSentryStructuredLogger>();
}

[Fact]
public async Task Logger_FlushAsync_DoesCaptureLog()
{
// Arrange
_fixture.Options.EnableLogs = true;
var hub = _fixture.GetSut();

// Act
Expand All @@ -2090,7 +2061,6 @@ await _fixture.Client.Received(1).FlushAsync(
public void Logger_Dispose_DoesCaptureLog()
{
// Arrange
_fixture.Options.EnableLogs = true;
var hub = _fixture.GetSut();

// Act
Expand Down
44 changes: 2 additions & 42 deletions test/Sentry.Tests/SentryStructuredLoggerTests.Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ public partial class SentryStructuredLoggerTests
[InlineData(SentryLogLevel.Warning)]
[InlineData(SentryLogLevel.Error)]
[InlineData(SentryLogLevel.Fatal)]
public void Log_Enabled_CapturesEnvelope(SentryLogLevel level)
public void Log_CapturesEnvelope(SentryLogLevel level)
{
_fixture.Options.EnableLogs = true;
var logger = _fixture.GetSut();

Envelope envelope = null!;
Expand All @@ -33,27 +32,8 @@ public void Log_Enabled_CapturesEnvelope(SentryLogLevel level)
[InlineData(SentryLogLevel.Warning)]
[InlineData(SentryLogLevel.Error)]
[InlineData(SentryLogLevel.Fatal)]
public void Log_Disabled_DoesNotCaptureEnvelope(SentryLogLevel level)
public void Log_ConfigureLog_CapturesEnvelope(SentryLogLevel level)
{
_fixture.Options.EnableLogs.Should().BeFalse();
var logger = _fixture.GetSut();

logger.Log(level, "Template string with arguments: {0}, {1}, {2}, {3}", "string", true, 1, 2.2);
logger.Flush();

_fixture.Hub.Received(0).CaptureEnvelope(Arg.Any<Envelope>());
}

[Theory]
[InlineData(SentryLogLevel.Trace)]
[InlineData(SentryLogLevel.Debug)]
[InlineData(SentryLogLevel.Info)]
[InlineData(SentryLogLevel.Warning)]
[InlineData(SentryLogLevel.Error)]
[InlineData(SentryLogLevel.Fatal)]
public void Log_ConfigureLog_Enabled_CapturesEnvelope(SentryLogLevel level)
{
_fixture.Options.EnableLogs = true;
var logger = _fixture.GetSut();

Envelope envelope = null!;
Expand All @@ -66,24 +46,6 @@ public void Log_ConfigureLog_Enabled_CapturesEnvelope(SentryLogLevel level)
_fixture.AssertEnvelope(envelope, level);
}

[Theory]
[InlineData(SentryLogLevel.Trace)]
[InlineData(SentryLogLevel.Debug)]
[InlineData(SentryLogLevel.Info)]
[InlineData(SentryLogLevel.Warning)]
[InlineData(SentryLogLevel.Error)]
[InlineData(SentryLogLevel.Fatal)]
public void Log_ConfigureLog_Disabled_DoesNotCaptureEnvelope(SentryLogLevel level)
{
_fixture.Options.EnableLogs.Should().BeFalse();
var logger = _fixture.GetSut();

logger.Log(level, ConfigureLog, "Template string with arguments: {0}, {1}, {2}, {3}", "string", true, 1, 2.2);
logger.Flush();

_fixture.Hub.Received(0).CaptureEnvelope(Arg.Any<Envelope>());
}

[Theory]
[InlineData(SentryLogLevel.Trace)]
[InlineData(SentryLogLevel.Debug)]
Expand All @@ -93,7 +55,6 @@ public void Log_ConfigureLog_Disabled_DoesNotCaptureEnvelope(SentryLogLevel leve
[InlineData(SentryLogLevel.Fatal)]
public void Log_WithoutParameters_DoesNotAttachTemplateAttribute(SentryLogLevel level)
{
_fixture.Options.EnableLogs = true;
var logger = _fixture.GetSut();

Envelope envelope = null!;
Expand Down Expand Up @@ -123,7 +84,6 @@ public void Log_WithoutParameters_DoesNotAttachTemplateAttribute(SentryLogLevel
[InlineData(SentryLogLevel.Fatal)]
public void Log_InvalidFormatButWithoutParameters_CapturesEnvelope(SentryLogLevel level)
{
_fixture.Options.EnableLogs = true;
var logger = _fixture.GetSut();

Envelope envelope = null!;
Expand Down
21 changes: 4 additions & 17 deletions test/Sentry.Tests/SentryStructuredLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ public void Dispose()
}

[Fact]
public void Create_Enabled_NewDefaultInstance()
public void Create_NewDefaultInstance()
{
_fixture.Options.EnableLogs = true;

var instance = _fixture.GetSut();
var other = _fixture.GetSut();

Expand All @@ -86,22 +84,20 @@ public void Create_Enabled_NewDefaultInstance()
}

[Fact]
public void Create_Disabled_CachedDisabledInstance()
public void Create_EnableLogsDisabled_NewDefaultInstance()
{
// EnableLogs gates the logging integrations, not this API.
_fixture.Options.EnableLogs.Should().BeFalse();

var instance = _fixture.GetSut();
var other = _fixture.GetSut();

instance.Should().BeOfType<DisabledSentryStructuredLogger>();
instance.Should().BeSameAs(other);
instance.Should().BeOfType<DefaultSentryStructuredLogger>();
}

[Fact]
public void Log_WithoutActiveSpan_CapturesEnvelope()
{
_fixture.WithoutActiveSpan();
_fixture.Options.EnableLogs = true;
var logger = _fixture.GetSut();

Envelope envelope = null!;
Expand All @@ -120,7 +116,6 @@ public void Log_WithBeforeSendLog_InvokesCallback()
var invocations = 0;
SentryLog configuredLog = null!;

_fixture.Options.EnableLogs = true;
_fixture.Options.SetBeforeSendLog((SentryLog log) =>
{
invocations++;
Expand All @@ -142,7 +137,6 @@ public void Log_WhenBeforeSendLogReturnsNull_DoesNotCaptureEnvelope()
{
var invocations = 0;

_fixture.Options.EnableLogs = true;
_fixture.Options.SetBeforeSendLog((SentryLog log) =>
{
invocations++;
Expand All @@ -159,7 +153,6 @@ public void Log_WhenBeforeSendLogReturnsNull_DoesNotCaptureEnvelope()
[Fact]
public void Log_InvalidFormat_DoesNotCaptureEnvelope()
{
_fixture.Options.EnableLogs = true;
var logger = _fixture.GetSut();

logger.LogTrace("Template string with arguments: {0}, {1}, {2}, {3}, {4}", "string", true, 1, 2.2);
Expand All @@ -175,7 +168,6 @@ public void Log_InvalidFormat_DoesNotCaptureEnvelope()
[Fact]
public void Log_InvalidConfigureLog_DoesNotCaptureEnvelope()
{
_fixture.Options.EnableLogs = true;
var logger = _fixture.GetSut();

logger.LogTrace(static (SentryLog log) => throw new InvalidOperationException(), "Template string with arguments: {0}, {1}, {2}, {3}", "string", true, 1, 2.2);
Expand All @@ -191,7 +183,6 @@ public void Log_InvalidConfigureLog_DoesNotCaptureEnvelope()
[Fact]
public void Log_InvalidBeforeSendLog_DoesNotCaptureEnvelope()
{
_fixture.Options.EnableLogs = true;
_fixture.Options.SetBeforeSendLog(static (SentryLog log) => throw new InvalidOperationException());
var logger = _fixture.GetSut();

Expand All @@ -208,7 +199,6 @@ public void Log_InvalidBeforeSendLog_DoesNotCaptureEnvelope()
[Fact]
public void Flush_AfterLog_CapturesEnvelope()
{
_fixture.Options.EnableLogs = true;
var logger = _fixture.GetSut();

Envelope envelope = null!;
Expand All @@ -230,7 +220,6 @@ public void Flush_AfterLog_CapturesEnvelope()
[Fact]
public void Dispose_BeforeLog_DoesNotCaptureEnvelope()
{
_fixture.Options.EnableLogs = true;
var logger = _fixture.GetSut();

var defaultLogger = logger.Should().BeOfType<DefaultSentryStructuredLogger>().Which;
Expand All @@ -253,7 +242,6 @@ public void Log_WithScopeUser_SetsUserAttributes()
_fixture.Hub.SubstituteConfigureScope(scope);

SentryLog capturedLog = null!;
_fixture.Options.EnableLogs = true;
_fixture.Options.SetBeforeSendLog((SentryLog log) =>
{
capturedLog = log;
Expand All @@ -277,7 +265,6 @@ public void Log_WithoutScopeUser_DoesNotSetUserAttributes()
_fixture.Hub.SubstituteConfigureScope(scope);

SentryLog capturedLog = null!;
_fixture.Options.EnableLogs = true;
_fixture.Options.SetBeforeSendLog((SentryLog log) =>
{
capturedLog = log;
Expand Down
Loading