Skip to content

Commit dadd070

Browse files
authored
chore: Setup logging only once (#4706)
1 parent e135645 commit dadd070

File tree

3 files changed

+82
-10
lines changed

3 files changed

+82
-10
lines changed

src/Sentry/SentryOptions.cs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,16 +1834,7 @@ internal void SetupLogging()
18341834
if (DiagnosticLogger == null)
18351835
{
18361836
DiagnosticLogger = new ConsoleDiagnosticLogger(DiagnosticLevel);
1837-
DiagnosticLogger.LogDebug("Logging enabled with ConsoleDiagnosticLogger and min level: {0}",
1838-
DiagnosticLevel);
1839-
}
1840-
1841-
if (SettingLocator.GetEnvironment().Equals("production", StringComparison.OrdinalIgnoreCase))
1842-
{
1843-
DiagnosticLogger.LogWarning("Sentry option 'Debug' is set to true while Environment is production. " +
1844-
"Be aware this can cause performance degradation and is not advised. " +
1845-
"See https://docs.sentry.io/platforms/dotnet/configuration/diagnostic-logger " +
1846-
"for more information");
1837+
DiagnosticLogger.LogDebug("Logging enabled with ConsoleDiagnosticLogger and min level: {0}", DiagnosticLevel);
18471838
}
18481839
}
18491840
else
@@ -1852,6 +1843,42 @@ internal void SetupLogging()
18521843
}
18531844
}
18541845

1846+
internal void LogDiagnosticWarning()
1847+
{
1848+
if (Debug && DiagnosticLogger is not null && SettingLocator.GetEnvironment().Equals("production", StringComparison.OrdinalIgnoreCase))
1849+
{
1850+
DiagnosticLogger.LogWarning("Sentry option 'Debug' is set to true while Environment is production. " +
1851+
"Be aware this can cause performance degradation and is not advised. " +
1852+
"See https://docs.sentry.io/platforms/dotnet/configuration/diagnostic-logger " +
1853+
"for more information");
1854+
}
1855+
}
1856+
1857+
internal string? TryGetDsnSpecificCacheDirectoryPath()
1858+
{
1859+
if (string.IsNullOrWhiteSpace(CacheDirectoryPath))
1860+
{
1861+
return null;
1862+
}
1863+
1864+
// DSN must be set to use caching
1865+
if (string.IsNullOrWhiteSpace(Dsn))
1866+
{
1867+
return null;
1868+
}
1869+
#if IOS || ANDROID // on iOS or Android the app is already sandboxed so there's no risk of sending data from 1 app to another Sentry's DSN
1870+
return Path.Combine(CacheDirectoryPath, "Sentry");
1871+
#else
1872+
return Path.Combine(CacheDirectoryPath, "Sentry", Dsn.GetHashString());
1873+
#endif
1874+
}
1875+
1876+
internal string? TryGetProcessSpecificCacheDirectoryPath()
1877+
{
1878+
// In the future, this will most likely contain process ID
1879+
return TryGetDsnSpecificCacheDirectoryPath();
1880+
}
1881+
18551882
internal static List<StringOrRegex> GetDefaultInAppExclude() =>
18561883
[
18571884
"System",

src/Sentry/SentrySdk.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ internal static IHub InitHub(SentryOptions options)
3333
options.InitCounter.Increment();
3434

3535
options.SetupLogging();
36+
options.LogDiagnosticWarning();
3637

3738
ProcessInfo.Instance ??= new ProcessInfo(options);
3839

test/Sentry.Tests/SentryClientTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,50 @@ public SentryClientTests(ITestOutputHelper output)
5252
_output = output;
5353
}
5454

55+
[Fact]
56+
public void Ctor_DebugTrue_CreatesConsoleDiagnosticLogger()
57+
{
58+
// Arrange
59+
_fixture.SentryOptions.Debug = true;
60+
_fixture.SentryOptions.DiagnosticLogger = null;
61+
62+
// Act
63+
_ = _fixture.GetSut();
64+
65+
// Assert
66+
Assert.NotNull(_fixture.SentryOptions.DiagnosticLogger);
67+
Assert.IsType<ConsoleDiagnosticLogger>(_fixture.SentryOptions.DiagnosticLogger);
68+
}
69+
70+
[Fact]
71+
public void Ctor_DebugFalseButLoggerSet_SetsLoggerToNull()
72+
{
73+
// Arrange
74+
_fixture.SentryOptions.Debug = false;
75+
_fixture.SentryOptions.DiagnosticLogger = Substitute.For<IDiagnosticLogger>();
76+
77+
// Act
78+
_ = _fixture.GetSut();
79+
80+
// Assert
81+
Assert.Null(_fixture.SentryOptions.DiagnosticLogger);
82+
}
83+
84+
[Fact]
85+
public void Ctor_DebugTrueAndLoggerSet_KeepsExistingLogger()
86+
{
87+
// Arrange
88+
var existingLogger = Substitute.For<IDiagnosticLogger>();
89+
_fixture.SentryOptions.Debug = true;
90+
_fixture.SentryOptions.DiagnosticLogger = existingLogger;
91+
92+
// Act
93+
_ = _fixture.GetSut();
94+
95+
// Assert
96+
Assert.Same(existingLogger, _fixture.SentryOptions.DiagnosticLogger);
97+
}
98+
5599
[Theory]
56100
[MemberData(nameof(GetExceptionFilterTestCases))]
57101
public void CaptureEvent_ExceptionFilteredForType(bool shouldFilter, Exception exception, params IExceptionFilter[] filters)

0 commit comments

Comments
 (0)