Class FakeLogCollectorWaitExtensions
- Namespace
- LogAssertions
- Assembly
- LogAssertions.dll
Event-driven waits for log records on a Microsoft.Extensions.Logging.Testing.FakeLogCollector: wait until background work has logged a matching record, instead of polling the collector or sleeping. Built on GetLogsAsync(CancellationToken), which yields the records already collected and then each new one as it is written, so a wait returns the moment the record arrives.
public static class FakeLogCollectorWaitExtensions
- Inheritance
-
FakeLogCollectorWaitExtensions
- Inherited Members
Remarks
Records already collected count: a wait for a record that was logged before the call returns at once. To wait only for records logged from now on, clear the collector first (Microsoft.Extensions.Logging.Testing.FakeLogCollector.Clear()), or use the count form and wait for one more than are already there.
The token is the only bound, so it is required: pass the test's own token, or a linked source with a timeout. When it is canceled, the wait throws OperationCanceledException with a message that says how many records arrived and how many matched.
A wait proves that a record arrived; it cannot prove that one did not. For "has not logged", let the work finish first (an event, a barrier) and then assert on the collector.
Methods
WaitForLogAsync(FakeLogCollector, ILogRecordFilter, CancellationToken)
Waits until collector holds a record that matches filter, and
returns the first such record.
public static Task<FakeLogRecord> WaitForLogAsync(this FakeLogCollector collector, ILogRecordFilter filter, CancellationToken cancellationToken)
Parameters
collectorFakeLogCollectorThe collector to wait on.
filterILogRecordFilterThe filter the record must match, for example
LogFilter.All(LogFilter.AtLevel(LogLevel.Warning), LogFilter.Containing("retry", StringComparison.Ordinal)).cancellationTokenCancellationTokenThe only bound of the wait, typically the test's token.
Returns
- Task<FakeLogRecord>
The first matching record.
Exceptions
- ArgumentNullException
collectororfilteris null.- OperationCanceledException
cancellationTokenwas canceled before a matching record arrived.
WaitForLogAsync(FakeLogCollector, Func<FakeLogRecord, bool>, CancellationToken)
Waits until collector holds a record that satisfies predicate,
and returns the first such record.
public static Task<FakeLogRecord> WaitForLogAsync(this FakeLogCollector collector, Func<FakeLogRecord, bool> predicate, CancellationToken cancellationToken)
Parameters
collectorFakeLogCollectorThe collector to wait on.
predicateFunc<FakeLogRecord, bool>The condition the record must satisfy.
cancellationTokenCancellationTokenThe only bound of the wait, typically the test's token.
Returns
- Task<FakeLogRecord>
The first matching record.
Exceptions
- ArgumentNullException
collectororpredicateis null.- OperationCanceledException
cancellationTokenwas canceled before a matching record arrived.
WaitForLogsAsync(FakeLogCollector, ILogRecordFilter, int, CancellationToken)
Waits until collector holds count records that match
filter, and returns the first count of them in the order they were
logged.
public static Task<IReadOnlyList<FakeLogRecord>> WaitForLogsAsync(this FakeLogCollector collector, ILogRecordFilter filter, int count, CancellationToken cancellationToken)
Parameters
collectorFakeLogCollectorThe collector to wait on.
filterILogRecordFilterThe filter the records must match.
countintThe number of matching records to wait for. Must be positive.
cancellationTokenCancellationTokenThe only bound of the wait, typically the test's token.
Returns
- Task<IReadOnlyList<FakeLogRecord>>
The first
countmatching records.
Exceptions
- ArgumentNullException
collectororfilteris null.- ArgumentOutOfRangeException
countis not positive.- OperationCanceledException
cancellationTokenwas canceled before enough matching records arrived.