-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
LoggingTests.cs
103 lines (91 loc) · 3.43 KB
/
LoggingTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Sentry.Extensions.Logging.Tests;
public class LoggingTests
{
[SkippableTheory]
[InlineData(LogLevel.Critical)]
[InlineData(LogLevel.Error)]
[InlineData(LogLevel.Warning)]
[InlineData(LogLevel.Information)]
[InlineData(LogLevel.Debug)]
[InlineData(LogLevel.Trace)]
public void Log_CapturesEvent(LogLevel logLevel)
{
#if __IOS__
Skip.If(true, "Flaky on iOS");
#endif
Skip.If(TestEnvironment.IsGitHubActions, "Flaky on CI");
// Arrange
var worker = Substitute.For<IBackgroundWorker>();
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(builder => builder.AddSentry(o =>
{
o.Dsn = ValidDsn;
o.MinimumEventLevel = logLevel;
o.MinimumBreadcrumbLevel = LogLevel.None;
o.BackgroundWorker = worker;
o.InitNativeSdks = false;
}));
using var serviceProvider = serviceCollection.BuildServiceProvider();
using var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
// Act
const string message = "test message";
var logger = loggerFactory.CreateLogger("test");
logger.Log(logLevel, message);
// Assert
worker.Received(1).EnqueueEnvelope(
Arg.Is<Envelope>(e =>
e.Items
.Select(i => i.Payload).OfType<JsonSerializable>()
.Select(i => i.Source).OfType<SentryEvent>()
.SingleOrDefault(evt =>
evt.Level == logLevel.ToSentryLevel() &&
evt.Message.Message == message)
!= null));
}
[SkippableTheory]
[InlineData(LogLevel.Critical)]
[InlineData(LogLevel.Error)]
[InlineData(LogLevel.Warning)]
[InlineData(LogLevel.Information)]
[InlineData(LogLevel.Debug)]
[InlineData(LogLevel.Trace)]
public void Log_AddsBreadcrumb(LogLevel logLevel)
{
#if __IOS__
Skip.If(true, "Flaky on iOS");
#endif
// Arrange
var worker = Substitute.For<IBackgroundWorker>();
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(builder => builder.AddSentry(o =>
{
o.Dsn = ValidDsn;
o.MinimumBreadcrumbLevel = logLevel;
o.MinimumEventLevel = LogLevel.None;
o.BackgroundWorker = worker;
o.InitNativeSdks = false;
}));
using var serviceProvider = serviceCollection.BuildServiceProvider();
using var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
// Act
const string message = "test message";
var logger = loggerFactory.CreateLogger("test");
logger.Log(logLevel, message);
var hub = serviceProvider.GetRequiredService<IHub>();
hub.CaptureEvent(new SentryEvent());
// Assert
worker.Received(1).EnqueueEnvelope(
Arg.Is<Envelope>(e =>
e.Items
.Select(i => i.Payload).OfType<JsonSerializable>()
.Select(i => i.Source).OfType<SentryEvent>()
.Single()
.Breadcrumbs
.SingleOrDefault(b =>
b.Level == logLevel.ToBreadcrumbLevel() &&
b.Message == message)
!= null));
}
}