Skip to content

Commit 5982a5f

Browse files
#140 Add tests and docs for Category Name
1 parent 2b78a93 commit 5982a5f

File tree

8 files changed

+168
-7
lines changed

8 files changed

+168
-7
lines changed

docs/docs/library/verify-extensions.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,17 @@ The verified output might look like this:
6060
]
6161
```
6262

63-
The default settings emit 5 things - The Sequence number, the log level, the category name (usually the name of the class that generated the log), the message template, and the exception if it exists. The reson it doesn't emit the formatted message or exception message by default is that can be nondeterministic.
63+
The default settings emit 5 things:
64+
* The Sequence number,
65+
* The log level,
66+
* The category name (usually the name of the class that generated the log),
67+
* The message template, and
68+
* the exception if it exists.
6469

65-
Nondeterministic values in the verified file produce brittle tests, so the parts of the log most likely to be brittle are ommited by default. So things like the formatted message or exception messages are not shown unless explicitly requested.
70+
The reson it doesn't emit the formatted message or exception message by default is that can be nondeterministic. Nondeterministic values in the verified file produce brittle tests, so the parts of the log most likely to be brittle are ommited by default. Therefore, things like the formatted message or exception messages are not shown unless explicitly requested.
6671

6772
## How do I set up to verify the things I want?
6873

6974
* Configure the [Sequence](verify/sequence.md)
70-
* Verify the [LogLevel](verify/loglevel.md)
71-
75+
* Verify the [LogLevel](verify/log-level.md)
76+
* Verify the [CategoryName](verify/category-name.md)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
layout: default
3+
title: Verify Logs - Category Name
4+
---
5+
6+
# Verifying the category name of log entries
7+
8+
Typically the category name will be the name of the type where the logger is used. e.g.
9+
10+
```csharp
11+
namespace MyApp.Services;
12+
13+
public class MyService
14+
{
15+
public MyService(ILogger<MyService> logger)
16+
{
17+
_logger = logger;
18+
}
19+
20+
public void DoTheThing()
21+
{
22+
_logger.LogInformation("Doing the thing.");
23+
}
24+
}
25+
```
26+
27+
For the above code the category name would be `MyApp.Services.MyService`.
28+
29+
## Configuring verifying the Category Name
30+
31+
The default value is `true`.
32+
33+
```csharp
34+
public async Task VerifyWarningLogsAreEmittedCorrectlyTestAsync()
35+
{
36+
// ... Code that produces logs
37+
38+
VerifySettings verifySettings = new VerifySettings()
39+
.AddCapturedLogs(new LoggingCaptureVerifySettings()
40+
{
41+
CategoryName = true,
42+
});
43+
44+
await Verifier.Verify(logs, verifySettings);
45+
}
46+
```
47+
48+
### Example output
49+
50+
#### CategoryName = true
51+
52+
```
53+
[
54+
{
55+
Sequence: 0,
56+
LogLevel: Information,
57+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyCategoryNameTests,
58+
MessageTemplate: This is the first log. It is from the test class's category.
59+
},
60+
{
61+
Sequence: 1,
62+
LogLevel: Information,
63+
CategoryName: custom-category-name,
64+
MessageTemplate: This is the second log. It has a custom category name.
65+
}
66+
]
67+
```
68+
69+
#### CategoryName = false
70+
71+
```
72+
[
73+
{
74+
Sequence: 0,
75+
LogLevel: Information,
76+
MessageTemplate: This is the first log. It is from the test class's category.
77+
},
78+
{
79+
Sequence: 1,
80+
LogLevel: Information,
81+
MessageTemplate: This is the second log. It has a custom category name.
82+
}
83+
]
84+
```

docs/docs/library/verify/loglevel.md renamed to docs/docs/library/verify/log-level.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ title: Verify Logs - Sequence
77

88
The `LoggingCaptureVerifySettings` has a Boolean `LogLevel` property that defines if the log level is to be verified or not.
99

10+
The default value is `true`.
11+
1012
```csharp
1113
public async Task VerifyWarningLogsAreEmittedCorrectlyTestAsync()
1214
{

docs/docs/library/verify/sequence.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The `LoggingCaptureVerifySettings` has a `Sequence` property that defines how th
1010
* `ShowAsConsecutive`: The sequence numbers increment consecutively in the verified files. i.e. 0, 1, 2, 3, 4, etc. regardless of whether any logs were filtered out.
1111
* `ShowAsCadence`: The sequence numbers maintain their cadence in the verified files. This allows you to see that there are skipped logs in the verified files that you are not verifying. Say the the original sequence is 25, 27, 32 then it will be verified as 0, 2, 7.
1212

13+
The default value is `ShowAsConsecutive`
14+
1315
```csharp
1416
public async Task VerifyWarningLogsAreEmittedCorrectlyTestAsync()
1517
{

docs/scripts/contributors.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
This is a list of all the contributors to this repository in ascending order by the contributor name.
44

5-
**Colin Mackay** contributed 352 commits from Saturday, 26 September, 2020 @ 20:26:19 +00:00 to Wednesday, 20 March, 2024 @ 22:53:33 +00:00.
5+
**Colin Mackay** contributed 353 commits from Saturday, 26 September, 2020 @ 20:26:19 +00:00 to Thursday, 21 March, 2024 @ 22:10:09 +00:00.
66

77
**dependabot[bot]** contributed 59 commits from Wednesday, 14 October, 2020 @ 19:13:43 +00:00 to Monday, 26 February, 2024 @ 14:54:09 +00:00.
88

99
**StravaigBot** contributed 28 commits from Monday, 14 December, 2020 @ 19:28:09 +00:00 to Tuesday, 27 February, 2024 @ 21:50:14 +00:00.
1010

1111
## Summary
1212

13-
:octocat: 439 commits in total.
13+
:octocat: 440 commits in total.
1414

1515
:date: From Saturday, 26 September, 2020 @ 20:26:19 +00:00.
1616

17-
:date: Until Wednesday, 20 March, 2024 @ 22:53:33 +00:00.
17+
:date: Until Thursday, 21 March, 2024 @ 22:10:09 +00:00.
1818

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
Sequence: 0,
4+
LogLevel: Information,
5+
MessageTemplate: This is the first log. It is from the test class's category.
6+
},
7+
{
8+
Sequence: 1,
9+
LogLevel: Information,
10+
MessageTemplate: This is the second log. It has a custom category name.
11+
}
12+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
Sequence: 0,
4+
LogLevel: Information,
5+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyCategoryNameTests,
6+
MessageTemplate: This is the first log. It is from the test class's category.
7+
},
8+
{
9+
Sequence: 1,
10+
LogLevel: Information,
11+
CategoryName: custom-category-name,
12+
MessageTemplate: This is the second log. It has a custom category name.
13+
}
14+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Collections.Generic;
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 VerifyCategoryNameTests
13+
{
14+
[Test]
15+
[TestCase(true)]
16+
[TestCase(false)]
17+
public async Task VerifyCategoryNameAsync(bool setting)
18+
{
19+
var logs = GetLogs();
20+
VerifySettings verifySettings = new VerifySettings()
21+
.AddCapturedLogs(new LoggingCaptureVerifySettings()
22+
{
23+
CategoryName = setting,
24+
});
25+
verifySettings.UseParameters(setting);
26+
27+
await Verifier.Verify(logs, verifySettings);
28+
}
29+
30+
private IReadOnlyList<LogEntry> GetLogs()
31+
{
32+
var provider = new TestCaptureLoggerProvider();
33+
LoggerFactory factory = new LoggerFactory([provider]);
34+
ILogger logger = factory.CreateLogger<VerifyCategoryNameTests>();
35+
logger.LogInformation("This is the first log. It is from the test class's category.");
36+
37+
logger = factory.CreateLogger("custom-category-name");
38+
logger.LogInformation("This is the second log. It has a custom category name.");
39+
40+
return provider.GetAllLogEntries();
41+
}
42+
}

0 commit comments

Comments
 (0)