Skip to content

Commit fa3bc2d

Browse files
authored
Add IDiagnosticLogger that routes to ITestOutputHelper (#621)
1 parent ba8e98e commit fa3bc2d

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using Sentry.Extensibility;
3+
using Xunit.Abstractions;
4+
5+
namespace Sentry.Testing
6+
{
7+
public class TestOutputDiagnosticLogger : IDiagnosticLogger
8+
{
9+
private readonly ITestOutputHelper _testOutputHelper;
10+
private readonly SentryLevel _minimumLevel;
11+
12+
public TestOutputDiagnosticLogger(
13+
ITestOutputHelper testOutputHelper,
14+
SentryLevel minimumLevel = SentryLevel.Debug)
15+
{
16+
_testOutputHelper = testOutputHelper;
17+
_minimumLevel = minimumLevel;
18+
}
19+
20+
public bool IsEnabled(SentryLevel level) => level >= _minimumLevel;
21+
22+
public void Log(SentryLevel logLevel, string message, Exception exception = null, params object[] args)
23+
{
24+
var formattedMessage = string.Format(message, args);
25+
26+
_testOutputHelper.WriteLine($@"
27+
[{logLevel}]: {formattedMessage}
28+
Exception: {exception?.ToString() ?? "<none>"}
29+
".Trim());
30+
}
31+
}
32+
}

test/Sentry.Tests/Internals/Http/CachingTransportTests.cs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,29 @@
1212
using Sentry.Protocol.Envelopes;
1313
using Sentry.Testing;
1414
using Xunit;
15+
using Xunit.Abstractions;
1516

1617
namespace Sentry.Tests.Internals.Http
1718
{
1819
public class CachingTransportTests
1920
{
21+
private readonly IDiagnosticLogger _logger;
22+
23+
public CachingTransportTests(ITestOutputHelper testOutputHelper)
24+
{
25+
_logger = new TestOutputDiagnosticLogger(testOutputHelper);
26+
}
27+
2028
[Fact(Timeout = 7000)]
2129
public async Task WorksInBackground()
2230
{
2331
// Arrange
2432
using var cacheDirectory = new TempDirectory();
25-
var options = new SentryOptions {CacheDirectoryPath = cacheDirectory.Path};
33+
var options = new SentryOptions
34+
{
35+
DiagnosticLogger = _logger,
36+
CacheDirectoryPath = cacheDirectory.Path
37+
};
2638

2739
using var innerTransport = new FakeTransport();
2840
await using var transport = new CachingTransport(innerTransport, options);
@@ -49,7 +61,11 @@ public async Task EnvelopeReachesInnerTransport()
4961
{
5062
// Arrange
5163
using var cacheDirectory = new TempDirectory();
52-
var options = new SentryOptions {CacheDirectoryPath = cacheDirectory.Path};
64+
var options = new SentryOptions
65+
{
66+
DiagnosticLogger = _logger,
67+
CacheDirectoryPath = cacheDirectory.Path
68+
};
5369

5470
using var innerTransport = new FakeTransport();
5571
await using var transport = new CachingTransport(innerTransport, options);
@@ -75,6 +91,7 @@ public async Task MaintainsLimit()
7591
using var cacheDirectory = new TempDirectory();
7692
var options = new SentryOptions
7793
{
94+
DiagnosticLogger = _logger,
7895
CacheDirectoryPath = cacheDirectory.Path,
7996
MaxQueueItems = 2
8097
};
@@ -105,7 +122,11 @@ public async Task AwareOfExistingFiles()
105122
{
106123
// Arrange
107124
using var cacheDirectory = new TempDirectory();
108-
var options = new SentryOptions {CacheDirectoryPath = cacheDirectory.Path};
125+
var options = new SentryOptions
126+
{
127+
DiagnosticLogger = _logger,
128+
CacheDirectoryPath = cacheDirectory.Path
129+
};
109130

110131
// Send some envelopes with a failing transport to make sure they all stay in cache
111132
{
@@ -144,7 +165,11 @@ public async Task DoesNotRetryOnNonTransientExceptions()
144165
{
145166
// Arrange
146167
using var cacheDirectory = new TempDirectory();
147-
var options = new SentryOptions {CacheDirectoryPath = cacheDirectory.Path};
168+
var options = new SentryOptions
169+
{
170+
DiagnosticLogger = _logger,
171+
CacheDirectoryPath = cacheDirectory.Path
172+
};
148173

149174
var innerTransport = Substitute.For<ITransport>();
150175
var isFailing = true;
@@ -186,7 +211,11 @@ public async Task DoesNotDeleteCacheIfConnectionWithIssue()
186211
{
187212
// Arrange
188213
using var cacheDirectory = new TempDirectory();
189-
var options = new SentryOptions { CacheDirectoryPath = cacheDirectory.Path };
214+
var options = new SentryOptions
215+
{
216+
DiagnosticLogger = _logger,
217+
CacheDirectoryPath = cacheDirectory.Path
218+
};
190219

191220
var exception = new HttpRequestException(null, new SocketException());
192221
var receivedException = new Exception();

test/Sentry.Tests/SentrySdkTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Sentry.Protocol.Envelopes;
1111
using Sentry.Testing;
1212
using Xunit;
13+
using Xunit.Abstractions;
1314
using static Sentry.Internal.Constants;
1415
using static Sentry.Protocol.Constants;
1516
using static Sentry.DsnSamples;
@@ -19,6 +20,13 @@ namespace Sentry.Tests
1920
[Collection(nameof(SentrySdkCollection))]
2021
public class SentrySdkTests : SentrySdkTestFixture
2122
{
23+
private readonly IDiagnosticLogger _logger;
24+
25+
public SentrySdkTests(ITestOutputHelper testOutputHelper)
26+
{
27+
_logger = new TestOutputDiagnosticLogger(testOutputHelper);
28+
}
29+
2230
[Fact]
2331
public void IsEnabled_StartsOfFalse()
2432
{
@@ -213,6 +221,7 @@ public async Task Init_WithCache_BlocksUntilExistingCacheIsFlushed()
213221
var initialInnerTransport = new FakeFailingTransport();
214222
await using var initialTransport = new CachingTransport(initialInnerTransport, new SentryOptions
215223
{
224+
DiagnosticLogger = _logger,
216225
Dsn = ValidDsnWithoutSecret,
217226
CacheDirectoryPath = cacheDirectory.Path
218227
});
@@ -232,6 +241,7 @@ public async Task Init_WithCache_BlocksUntilExistingCacheIsFlushed()
232241
using var _ = SentrySdk.Init(o =>
233242
{
234243
o.Dsn = ValidDsnWithoutSecret;
244+
o.DiagnosticLogger = _logger;
235245
o.CacheDirectoryPath = cacheDirectory.Path;
236246
o.CacheFlushTimeout = TimeSpan.FromSeconds(30);
237247
o.CreateHttpClientHandler = () => new FakeHttpClientHandler();

0 commit comments

Comments
 (0)