Skip to content

Commit b3c493c

Browse files
#140: Add tests for message setting + docs
1 parent 5982a5f commit b3c493c

6 files changed

+149
-0
lines changed

docs/docs/library/verify-extensions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,4 @@ The reson it doesn't emit the formatted message or exception message by default
7474
* Configure the [Sequence](verify/sequence.md)
7575
* Verify the [LogLevel](verify/log-level.md)
7676
* Verify the [CategoryName](verify/category-name.md)
77+
* Verify the log [Message](verify/message.md)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
layout: default
3+
title: Verify Logs - Message Settings
4+
---
5+
6+
# Verify the log message
7+
8+
Typically, this is the most important part of the log entry. By default the message template is verified.
9+
10+
There are three settings:
11+
* `None`: Does not verify the log message.
12+
* `Formatted`: Verifies the formatted log message with the values filled in. Depending on what the values are this may produce nondeterminisitc output.
13+
* `Template`: Verifies the log template.
14+
15+
The default value is `Template`.
16+
17+
```csharp
18+
public async Task VerifyWarningLogsAreEmittedCorrectlyTestAsync()
19+
{
20+
// ... Code that produces logs
21+
22+
VerifySettings verifySettings = new VerifySettings()
23+
.AddCapturedLogs(new LoggingCaptureVerifySettings()
24+
{
25+
Message = MessageSetting.Formatted,
26+
});
27+
28+
await Verifier.Verify(logs, verifySettings);
29+
}
30+
```
31+
32+
## Example Output
33+
34+
### None
35+
36+
```
37+
[
38+
{
39+
Sequence: 0,
40+
LogLevel: Information,
41+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyMessageSettingTests
42+
}
43+
]
44+
```
45+
46+
### Formatted
47+
```
48+
[
49+
{
50+
Sequence: 0,
51+
LogLevel: Information,
52+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyMessageSettingTests,
53+
FormattedMessage: This is a test of the Formatted message setting.
54+
}
55+
]
56+
```
57+
58+
### Template
59+
60+
```
61+
[
62+
{
63+
Sequence: 0,
64+
LogLevel: Information,
65+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyMessageSettingTests,
66+
MessageTemplate: This is a test of the {SettingName} message setting.
67+
}
68+
]
69+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
Sequence: 0,
4+
LogLevel: Information,
5+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyMessageSettingTests,
6+
FormattedMessage: This is a test of the Formatted message setting.
7+
},
8+
{
9+
Sequence: 1,
10+
LogLevel: Information,
11+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyMessageSettingTests,
12+
FormattedMessage:
13+
This is a multiline log message.
14+
This is the second line of the message.
15+
}
16+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
Sequence: 0,
4+
LogLevel: Information,
5+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyMessageSettingTests
6+
},
7+
{
8+
Sequence: 1,
9+
LogLevel: Information,
10+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyMessageSettingTests
11+
}
12+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
Sequence: 0,
4+
LogLevel: Information,
5+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyMessageSettingTests,
6+
MessageTemplate: This is a test of the {SettingName} message setting.
7+
},
8+
{
9+
Sequence: 1,
10+
LogLevel: Information,
11+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyMessageSettingTests,
12+
MessageTemplate:
13+
This is a multiline log message.
14+
This is the second line of the message.
15+
}
16+
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Logging;
4+
using NUnit.Framework;
5+
using Stravaig.Extensions.Logging.Diagnostics.Verify;
6+
using VerifyNUnit;
7+
using VerifyTests;
8+
9+
namespace Stravaig.Extensions.Logging.Diagnostics.Tests.Verify;
10+
11+
[TestFixture]
12+
public class VerifyMessageSettingTests
13+
{
14+
[Test]
15+
[TestCase(MessageSetting.None)]
16+
[TestCase(MessageSetting.Formatted)]
17+
[TestCase(MessageSetting.Template)]
18+
public async Task VerifyMessageSettingAsync(MessageSetting setting)
19+
{
20+
var logger = new TestCaptureLogger<VerifyMessageSettingTests>();
21+
logger.LogInformation("This is a test of the {SettingName} message setting.", setting);
22+
logger.LogInformation("This is a multiline log message.\nThis is the second line of the message.");
23+
var logs = logger.GetLogs();
24+
25+
VerifySettings verifySettings = new VerifySettings()
26+
.AddCapturedLogs(new LoggingCaptureVerifySettings()
27+
{
28+
Message = setting,
29+
});
30+
verifySettings.UseParameters(setting);
31+
32+
await Verifier.Verify(logs, verifySettings);
33+
34+
}
35+
}

0 commit comments

Comments
 (0)