Table of Contents

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

collector FakeLogCollector

The collector to wait on.

filter ILogRecordFilter

The filter the record must match, for example LogFilter.All(LogFilter.AtLevel(LogLevel.Warning), LogFilter.Containing("retry", StringComparison.Ordinal)).

cancellationToken CancellationToken

The only bound of the wait, typically the test's token.

Returns

Task<FakeLogRecord>

The first matching record.

Exceptions

ArgumentNullException

collector or filter is null.

OperationCanceledException

cancellationToken was 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

collector FakeLogCollector

The collector to wait on.

predicate Func<FakeLogRecord, bool>

The condition the record must satisfy.

cancellationToken CancellationToken

The only bound of the wait, typically the test's token.

Returns

Task<FakeLogRecord>

The first matching record.

Exceptions

ArgumentNullException

collector or predicate is null.

OperationCanceledException

cancellationToken was 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

collector FakeLogCollector

The collector to wait on.

filter ILogRecordFilter

The filter the records must match.

count int

The number of matching records to wait for. Must be positive.

cancellationToken CancellationToken

The only bound of the wait, typically the test's token.

Returns

Task<IReadOnlyList<FakeLogRecord>>

The first count matching records.

Exceptions

ArgumentNullException

collector or filter is null.

ArgumentOutOfRangeException

count is not positive.

OperationCanceledException

cancellationToken was canceled before enough matching records arrived.