Table of Contents

Class HasLoggedOnlyAssertion

Namespace
LogAssertions.TUnit
Assembly
LogAssertions.TUnit.dll

TUnit assertion that gates a test run on its log output: every captured record at or above floor must have been produced by one of the allowed definitions. Records below the floor are always permitted and are never enumerated, which is what makes the gate usable on a real service: the interesting band (warnings and errors) is small and can be listed, while the Debug/Trace volume (heartbeats, request bodies, request tracing) is not.

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

Examples

// "nothing at Warning-or-above escaped, except these two known-good events"
await Assert.That(collector).HasLoggedOnly(LogLevel.Warning)
    .Allowing(
        UpstreamContractViolated,   // deliberately emitted; flags an upstream bug
        StaleSessionDropped);      // benign post-restart close

With no allowed definitions the gate asserts that nothing was logged at or above the floor at all, which is the "clean run" check:

await Assert.That(collector).HasLoggedOnly(LogLevel.Warning);

Remarks

This is the complement of the rest of the DSL. HasLogged() asserts an expected record is present; the gate asserts no unexpected record is. Assert it once per test class or fixture (a TUnit [After] hook over the fixture's collector is the natural home) rather than per test.

Constructors

HasLoggedOnlyAssertion(AssertionContext<FakeLogCollector>, LogLevel)

Initialises the gate at floor. Called by the TUnit source generator. With no Allowing(params LogDefinition[]) call the gate asserts nothing was logged at or above the floor at all.

public HasLoggedOnlyAssertion(AssertionContext<FakeLogCollector> context, LogLevel floor)

Parameters

context AssertionContext<FakeLogCollector>

The assertion context supplied by TUnit.

floor LogLevel

The lowest level the gate inspects. Records below it are always allowed.

Methods

Allowing(params LogDefinition[])

Permits the given definitions at or above the floor. Call it once with every known-good event, or repeatedly to accumulate: .HasLoggedOnly(LogLevel.Warning).Allowing(UpstreamContractViolated, StaleSessionDropped).

public HasLoggedOnlyAssertion Allowing(params LogDefinition[] definitions)

Parameters

definitions LogDefinition[]

The definitions permitted at or above the floor. Must be non-null and contain no nulls.

Returns

HasLoggedOnlyAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

definitions is null.

ArgumentException

definitions contains a null definition.

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

GetExpectation()

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

protected override string GetExpectation()

Returns

string