Diagnostics for examining logs in automated tests.
The provider passed to the logging framework.
This class implements
ICapturedLogs
ILoggingProvider
Creates or returns an exsting TestCaptureLogger<T>
. This is a generic alternative to the non-generic version from ILoggerProvider
.
Gets all the logger categories created by this provider. NOTE: There is the possibility that the category may not contain any logs.
1
2
3
4
5
6
7
8
// Arrange
var logProvider = new TestCaptureLoggerProvider();
// Act... Do something using the logProvider
// Assert
var categories = logProvider.GetCategories();
// categories is a read only list of strings.
An IReadOnlyList<string>
representing a list of categories created in the provider.
Gets the log entries for the specified category.
GetLogEntriesFor(string categoryName)
GetLogEntriesFor(type type)
GetLogEntriesFor<T>()
IReadOnlyList<LogEntry>
: A list of log entries in sequence. See LogEntry.
The result of the method can be passed into RenderLogs()
extension method. See Renderer
Gets all the LogEntry
` objects generated via this provider in sequential order.
1
2
3
4
5
6
7
8
// Arrange
var logProvider = new TestCaptureLoggerProvider();
// Act... Do something using the logProvider
// Assert
var allLogs = logProvider.GetAllLogEntries();
// allLogs is a read only list of LogEntry objects in sequential order.
IReadOnlyList<LogEntry>
: A list of log entries in sequence. See LogEntry.
The result of the method can be passed into RenderLogs()
extension method. See Renderer
The TestCaptureLoggerProvider.GetLogs(predicate)
allows you to retrieve specific logs within your test method that match the predicate. The log entries will be in sequence, timestamps will be incremental, however adjacent log entries created sufficiently close to one another may contain the same timestamp due to the resolution of the clock.
IReadOnlyList<LogEntry>
: A read only list of log entries. See LogEntry
This example checks that a specific log entry was generated.
1
2
3
4
5
6
7
8
9
10
// Arrange
var logProvider = new TestCaptureLoggerProvider();
// Act: Do something using the log provider.
// Assert
var logs = logProvider.GetLogs(
static le => le.LogLevel == LogLevel.Warning &&
le.OriginalMessage == "A thing happened.");
logs.Count.ShouldBe(1);
Gets all the log entries generated via this provider in sequential order that have exception objects attached to them.
1
2
3
4
5
6
7
8
// Arrange
var logProvider = new TestCaptureLoggerProvider();
// Act... Do something using the logProvider
// Assert
var logs = logProvider.GetAllLogEntriesWithExceptions();
// logs is a read only list of LogEntry objects in sequential order.
IReadOnlyList<LogEntry>
: A list of log entries in sequence. See LogEntry.
The result of the method can be passed into RenderLogs()
extension method. See Renderer
Resets the provider and discards all the logs captured to that point.
1
2
3
4
5
6
7
var logProvider = new TestCaptureLoggerProvider();
// Create log entries here...
logProvider.Reset();
var logs = logProvider.GetAllLogEntries();
logs.Count.ShouldBe(0); // logs is an empty list.