Table of Contents

Class HasLoggedAssertion

Namespace
LogAssertions.TUnit
Assembly
LogAssertions.TUnit.dll

TUnit assertion that verifies a Microsoft.Extensions.Logging.Testing.FakeLogCollector contains matching log records. Inherits filter chaining from LogAssertionBase<TSelf>; adds count-expectation terminators (Once, Exactly, AtLeast, AtMost, Never) and the value-returning terminators GetMatch and GetMatches for handing the matched records to follow-up assertions.

[AssertionExtension("HasLogged")]
public sealed class HasLoggedAssertion : LogAssertionBase<HasLoggedAssertion>, IAssertion
Inheritance
Assertion<FakeLogCollector>
HasLoggedAssertion
Implements
IAssertion
Inherited Members
Assertion<FakeLogCollector>.AssertAsync()
Assertion<FakeLogCollector>.GetAwaiter()
Assertion<FakeLogCollector>.And
Assertion<FakeLogCollector>.Or

Constructors

HasLoggedAssertion(AssertionContext<FakeLogCollector>)

Initialises a positive log assertion. Called by the TUnit source generator.

public HasLoggedAssertion(AssertionContext<FakeLogCollector> context)

Parameters

context AssertionContext<FakeLogCollector>

The assertion context supplied by TUnit.

Methods

AtLeast(int)

Expects at least count matching records.

public HasLoggedAssertion AtLeast(int count)

Parameters

count int

The minimum match count. Must be non-negative.

Returns

HasLoggedAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

count is negative.

AtMost(int)

Expects at most count matching records.

public HasLoggedAssertion AtMost(int count)

Parameters

count int

The maximum match count. Must be non-negative.

Returns

HasLoggedAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

count is negative.

Between(int, int)

Expects the matching record count to fall in the inclusive range [min, max].

public HasLoggedAssertion Between(int min, int max)

Parameters

min int

The minimum match count (inclusive). Must be non-negative.

max int

The maximum match count (inclusive). Must be greater than or equal to min.

Returns

HasLoggedAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

min is negative, or max is less than min.

CheckAsync(EvaluationMetadata<FakeLogCollector>)

Implements the specific check logic for this assertion. Called after the context has been evaluated. Override this method if your assertion uses the default AssertAsync() flow. If you override AssertAsync() with custom logic (like AndAssertion/OrAssertion), you don't need to implement this.

protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<FakeLogCollector> metadata)

Parameters

metadata EvaluationMetadata<FakeLogCollector>

Metadata about the evaluation including value, exception, and timing information

Returns

Task<AssertionResult>

The result of the assertion check

Exactly(int)

Expects exactly count matching records.

public HasLoggedAssertion Exactly(int count)

Parameters

count int

The required match count. Must be non-negative.

Returns

HasLoggedAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

count is negative.

GetExpectation()

Gets a human-readable description of what this assertion expects. Used in error messages.

protected override string GetExpectation()

Returns

string

GetMatch()

Returns the single matched record once the assertion passes. Requires the chain's count expectation to be exactly one: typically expressed via Once() or Exactly(int) with count 1, but any terminator that constrains the count to exactly one (including Between(int, int) with both bounds equal to 1) is also accepted. Throws InvalidOperationException for any other expectation so the call site fails fast on a nonsensical "give me the single match" against a chain that allows N matches.

public Task<FakeLogRecord> GetMatch()

Returns

Task<FakeLogRecord>

The single matched record.

Exceptions

InvalidOperationException

The chain's count expectation does not constrain the match count to exactly one.

GetMatches()

Returns the matched records once the assertion passes. Awaits the chain (which evaluates the count expectation and throws on mismatch); on success, returns the snapshot of matches captured at evaluation time. Useful for handing the matched records to follow-up assertions without a duplicate collector.Filter(...) call.

public Task<IReadOnlyList<FakeLogRecord>> GetMatches()

Returns

Task<IReadOnlyList<FakeLogRecord>>

The matched records in original order; the list is a snapshot, not bound to the live collector.

Never()

Expects zero matching records.

public HasLoggedAssertion Never()

Returns

HasLoggedAssertion

This assertion for chaining.

Once()

Expects exactly one matching record.

public HasLoggedAssertion Once()

Returns

HasLoggedAssertion

This assertion for chaining.