-
Notifications
You must be signed in to change notification settings - Fork 5
/
LoggingTest.cs
205 lines (166 loc) · 7.06 KB
/
LoggingTest.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
//using Microsoft.Extensions.Logging; // for AddSerilogTest
using Serilog;
using Serilog.Events;
using Xunit;
namespace SampleWebApplicationSerilogAlternate.IntegrationTests
{
[Collection("Serilog Test Collection")]
public class LoggingTest : IClassFixture<WebApplicationFactory<Startup>>, IDisposable
{
private readonly WebApplicationFactory<Startup> _factory;
public LoggingTest(WebApplicationFactory<Startup> factory)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.FromLogContext()
.WriteTo.Providers(Program.Providers)
.CreateLogger();
_factory = factory.WithWebHostBuilder(builder => builder
.UseSerilogTestLogging(options => options.FilterByNamespace(nameof(SampleWebApplicationSerilogAlternate))));
//or
//_factory = factory.WithWebHostBuilder(builder => builder
// .ConfigureLogging(logging => logging.AddSerilogTest(options => options.FilterByNamespace(nameof(SampleWebApplicationSerilogAlternate)))));
}
[Fact]
public async Task ShouldLogHelloWorld()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/");
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
// Assert the message rendered by a default formatter
Assert.Equal("Hello \"World\"!", log.Message);
}
[Fact]
public async Task ShouldLogWithWorldAsPlace()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/");
// Assert
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
// Assert specific parameters in the log entry
LoggingAssert.Contains("place", "World", log.Properties);
}
[Fact]
public async Task ShouldUseScope()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/");
// Assert
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
var scope = Assert.Single(log.Scopes);
Assert.Equal(new ScalarValue("I'm in the GET scope"), scope);
}
[Fact]
public async Task ShouldUseNestedScope()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/nestedScope");
// Assert
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
// Assert the scopes
Assert.Collection(log.Scopes,
x => Assert.Equal(new ScalarValue("A top level scope"), x),
x => Assert.Equal(new ScalarValue("I'm in the GET scope"), x)
);
// or
Assert.Equal(2, log.Scopes.Count);
Assert.Equal(new ScalarValue("A top level scope"), log.Scopes[0]);
Assert.Equal(new ScalarValue("I'm in the GET scope"), log.Scopes[1]);
}
[Fact]
public async Task ShouldUseDictionaryScope()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/dictionaryScope");
// Assert
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
// If the scope is a dictionary, it will only add the properties, not a scope
Assert.Empty(log.Scopes);
LoggingAssert.Contains("foo", "bar", log.Properties);
LoggingAssert.Contains("answer", 42, log.Properties);
}
[Fact]
public async Task ShouldUseScopeWithParameter()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/");
// Assert
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
// Assert specific parameters in the log entry itself, as Serilog puts the scope parameters on the log entry
LoggingAssert.Contains("name", "GET", log.Properties);
}
[Fact]
public async Task ShouldHaveDestructuredObjectInLogMessage()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/destructure");
// Assert
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
// Assert the message rendered by a default formatter
Assert.Equal("This { foo: \"bar\", answer: 42 } has been destructured.", log.Message);
}
[Fact]
public async Task ShouldHaveDestructuredProperty()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/destructure");
// Assert
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
// Assert specific parameters in the log entry
//StructureValue expected = new StructureValue(new[] {
// new LogEventProperty("foo", new ScalarValue("bar")),
// new LogEventProperty("answer", new ScalarValue(42)),
//});
//LogValuesAssert.Contains("thing", expected, log);
// or
//LogValuesAssert.Contains("thing", "{ foo: \"bar\", answer: 42 }", log);
var thing = Assert.Single(log.Properties, x => x.Key == "thing");
var value = Assert.IsType<StructureValue>(thing.Value);
var foo = Assert.Single(value.Properties, x => x.Name == "foo");
Assert.Equal(new ScalarValue("bar"), foo.Value);
var answer = Assert.Single(value.Properties, x => x.Name == "answer");
Assert.Equal(new ScalarValue(42), answer.Value);
}
[Fact]
public async Task ShouldHaveArrayObjectInLogMessage()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/array");
// Assert
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
// Assert the message rendered by a default formatter
Assert.Equal("This [1, 2] is an array.", log.Message);
}
[Fact]
public async Task ShouldHaveArrayProperty()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/array");
// Assert
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
var array = Assert.Single(log.Properties, x => x.Key == "array");
var sequence = Assert.IsType<SequenceValue>(array.Value);
Assert.Collection(sequence.Elements,
x => Assert.Equal(new ScalarValue(1), x),
x => Assert.Equal(new ScalarValue(2), x));
}
public void Dispose()
{
Log.CloseAndFlush();
}
}
}