From b7d4dcad46d8b7b9309ab239e83f3da80d7e2c28 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Fri, 7 Nov 2025 10:35:14 +0100 Subject: [PATCH 1/2] Only setup if no logger --- src/Sentry/SentryClient.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Sentry/SentryClient.cs b/src/Sentry/SentryClient.cs index 0ef816fcd5..022a6b12a0 100644 --- a/src/Sentry/SentryClient.cs +++ b/src/Sentry/SentryClient.cs @@ -51,7 +51,10 @@ internal SentryClient( _sessionManager = sessionManager ?? new GlobalSessionManager(options); _enricher = new Enricher(options); - options.SetupLogging(); // Only relevant if this client wasn't created as a result of calling Init + if (options.DiagnosticLogger is null) + { + options.SetupLogging(); // Only relevant if this client wasn't created as a result of calling Init + } if (worker == null) { From db3196c2e6b689b0ffc3ef4f3cdf44df52fcb16e Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Fri, 7 Nov 2025 12:44:24 +0100 Subject: [PATCH 2/2] Moved the warning out of setup --- src/Sentry/SentryClient.cs | 5 +-- src/Sentry/SentryOptions.cs | 22 +++++++------ src/Sentry/SentrySdk.cs | 1 + test/Sentry.Tests/SentryClientTests.cs | 44 ++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/Sentry/SentryClient.cs b/src/Sentry/SentryClient.cs index 022a6b12a0..0ef816fcd5 100644 --- a/src/Sentry/SentryClient.cs +++ b/src/Sentry/SentryClient.cs @@ -51,10 +51,7 @@ internal SentryClient( _sessionManager = sessionManager ?? new GlobalSessionManager(options); _enricher = new Enricher(options); - if (options.DiagnosticLogger is null) - { - options.SetupLogging(); // Only relevant if this client wasn't created as a result of calling Init - } + options.SetupLogging(); // Only relevant if this client wasn't created as a result of calling Init if (worker == null) { diff --git a/src/Sentry/SentryOptions.cs b/src/Sentry/SentryOptions.cs index 67e7248f11..bfaeb76f99 100644 --- a/src/Sentry/SentryOptions.cs +++ b/src/Sentry/SentryOptions.cs @@ -1791,16 +1791,7 @@ internal void SetupLogging() if (DiagnosticLogger == null) { DiagnosticLogger = new ConsoleDiagnosticLogger(DiagnosticLevel); - DiagnosticLogger.LogDebug("Logging enabled with ConsoleDiagnosticLogger and min level: {0}", - DiagnosticLevel); - } - - if (SettingLocator.GetEnvironment().Equals("production", StringComparison.OrdinalIgnoreCase)) - { - DiagnosticLogger.LogWarning("Sentry option 'Debug' is set to true while Environment is production. " + - "Be aware this can cause performance degradation and is not advised. " + - "See https://docs.sentry.io/platforms/dotnet/configuration/diagnostic-logger " + - "for more information"); + DiagnosticLogger.LogDebug("Logging enabled with ConsoleDiagnosticLogger and min level: {0}", DiagnosticLevel); } } else @@ -1809,6 +1800,17 @@ internal void SetupLogging() } } + internal void LogDiagnosticWarning() + { + if (Debug && DiagnosticLogger is not null && SettingLocator.GetEnvironment().Equals("production", StringComparison.OrdinalIgnoreCase)) + { + DiagnosticLogger.LogWarning("Sentry option 'Debug' is set to true while Environment is production. " + + "Be aware this can cause performance degradation and is not advised. " + + "See https://docs.sentry.io/platforms/dotnet/configuration/diagnostic-logger " + + "for more information"); + } + } + internal string? TryGetDsnSpecificCacheDirectoryPath() { if (string.IsNullOrWhiteSpace(CacheDirectoryPath)) diff --git a/src/Sentry/SentrySdk.cs b/src/Sentry/SentrySdk.cs index 10b5678d87..46da99658e 100644 --- a/src/Sentry/SentrySdk.cs +++ b/src/Sentry/SentrySdk.cs @@ -31,6 +31,7 @@ static partial class SentrySdk internal static IHub InitHub(SentryOptions options) { options.SetupLogging(); + options.LogDiagnosticWarning(); ProcessInfo.Instance ??= new ProcessInfo(options); diff --git a/test/Sentry.Tests/SentryClientTests.cs b/test/Sentry.Tests/SentryClientTests.cs index 0bf106fe3f..1014a4f011 100644 --- a/test/Sentry.Tests/SentryClientTests.cs +++ b/test/Sentry.Tests/SentryClientTests.cs @@ -52,6 +52,50 @@ public SentryClientTests(ITestOutputHelper output) _output = output; } + [Fact] + public void Ctor_DebugTrue_CreatesConsoleDiagnosticLogger() + { + // Arrange + _fixture.SentryOptions.Debug = true; + _fixture.SentryOptions.DiagnosticLogger = null; + + // Act + _ = _fixture.GetSut(); + + // Assert + Assert.NotNull(_fixture.SentryOptions.DiagnosticLogger); + Assert.IsType(_fixture.SentryOptions.DiagnosticLogger); + } + + [Fact] + public void Ctor_DebugFalseButLoggerSet_SetsLoggerToNull() + { + // Arrange + _fixture.SentryOptions.Debug = false; + _fixture.SentryOptions.DiagnosticLogger = Substitute.For(); + + // Act + _ = _fixture.GetSut(); + + // Assert + Assert.Null(_fixture.SentryOptions.DiagnosticLogger); + } + + [Fact] + public void Ctor_DebugTrueAndLoggerSet_KeepsExistingLogger() + { + // Arrange + var existingLogger = Substitute.For(); + _fixture.SentryOptions.Debug = true; + _fixture.SentryOptions.DiagnosticLogger = existingLogger; + + // Act + _ = _fixture.GetSut(); + + // Assert + Assert.Same(existingLogger, _fixture.SentryOptions.DiagnosticLogger); + } + [Theory] [MemberData(nameof(GetExceptionFilterTestCases))] public void CaptureEvent_ExceptionFilteredForType(bool shouldFilter, Exception exception, params IExceptionFilter[] filters)