Class LogCapture
- Namespace
- LogAssertions
- Assembly
- LogAssertions.dll
One test's log capture: a logger factory whose records go to Collector. The capture is the factory
itself, so it can be passed wherever an ILoggerFactory is expected, and one using
disposes everything. Created by Create(LogLevel) (and, in the TUnit adapter, by
TestOutputLogCollectorBuilder.CreateTeed).
public sealed class LogCapture : ILoggerFactory, IDisposable, IAsyncDisposable
- Inheritance
-
LogCapture
- Implements
- Inherited Members
Examples
using var logs = LogCollectorBuilder.Create();
var service = new OrderService(logs.CreateLogger<OrderService>());
service.Ship(42);
await Assert.That(logs.Collector).HasLogged().Matching(OrderShipped).Once();
Remarks
Disposing the capture disposes the factory and its providers; it is safe to call more than once. The collector keeps the records it already holds, so assertions can still run after disposal.
Properties
Collector
Gets the collector that receives every record logged through this capture's loggers.
public FakeLogCollector Collector { get; }
Property Value
- FakeLogCollector
Methods
AddProvider(ILoggerProvider)
Adds a provider to the capture's factory. Disposing the capture also disposes a provider added this way, as AddProvider(ILoggerProvider) does.
public void AddProvider(ILoggerProvider provider)
Parameters
providerILoggerProviderThe provider to add.
Exceptions
- ArgumentNullException
provideris null.- ObjectDisposedException
The capture was disposed.
CreateLogger(string)
Creates a logger whose records go to Collector.
public ILogger CreateLogger(string categoryName)
Parameters
categoryNamestringThe category name for the logger's records.
Returns
- ILogger
The logger.
Exceptions
- ArgumentNullException
categoryNameis null.- ObjectDisposedException
The capture was disposed.
Dispose()
Disposes the factory and the providers it owns. The collector keeps its records.
public void Dispose()
DisposeAsync()
Disposes the factory and the providers it owns, as Dispose() does; for await using and fixtures that are disposed asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A completed task.