Class LogSnapshotRenderer
- Namespace
- LogAssertions.Render
- Assembly
- LogAssertions.dll
Pure renderer that converts a Microsoft.Extensions.Logging.Testing.FakeLogCollector's captured records into
deterministic, snapshot-friendly multi-line text. Each record renders as a header line
([NN] level category "message") followed by optional indented detail lines for
structured state, active scopes, and an attached exception. Records are separated by a
single blank line.
public static class LogSnapshotRenderer
- Inheritance
-
LogSnapshotRenderer
- Inherited Members
Remarks
Stability contract. Unlike LogAssertionRendering (whose output is explicitly documented as not stable and is meant only for failure messages), the output of this renderer is a stable, pin-able contract. Snapshot baselines produced by Render(FakeLogCollector, LogSnapshotOptions?) are intended to be committed and diffed. A change to the rendered shape is a versioned, documented change.
Pairs with snapshot assertions. Render once, then pin the result against a baseline:
var rendered = LogSnapshotRenderer.Render(collector);
await Assert.That(rendered).MatchesSnapshot();
The MatchesSnapshot() extension lives in the sibling SnapshotAssertions.TUnit
package; this package does not depend on it. The two-line composition is deliberate: it
lets consumers reach for the renderer without committing to a specific snapshot framework,
and keeps the snapshot package an opt-in pairing rather than a transitive dependency.
No baked-in scrubbing. This renderer emits the captured text verbatim; it does not
scrub GUIDs, timestamps, or durations out of message bodies. Volatile values are the
concern of the snapshot layer: compose SnapshotAssertions.Scrubbers at the
MatchesSnapshot() call site. Keeping rendering and scrubbing separate means the
renderer has one job, the scrubber set stays in one place, and neither has to know about
the other.
Deterministic line endings. Lines are terminated with the literal LF byte
('\n'), never NewLine. The CRLF / LF split between Windows
and Unix would otherwise serialise the same records differently per OS, breaking snapshot
baselines on cross-platform CI. Hardcoding LF keeps a baseline committed on one platform
valid for test runs on every other.
Capture-order preserving. Records render in the order Microsoft.Extensions.Logging.Testing.FakeLogCollector captured them; the renderer never sorts. A regression that keeps per-record predicate assertions passing while reordering, dropping, or inserting records still surfaces as a snapshot diff.
Defensive structured-state handling. The renderer reads Microsoft.Extensions.Logging.Testing.FakeLogRecord
state through the Microsoft.Extensions.Logging.Testing.FakeLogRecord.StructuredState property inside a
try/catch for InvalidCastException. That
property hard-casts the captured state to a key-value-pair list and throws when it is not
one (e.g. ILogger.Log<TState> with a custom typed state); a record with
non-KVP state simply renders without a state: line instead of throwing.
Methods
Render(FakeLogCollector, LogSnapshotOptions?)
Renders every captured record from collector into a deterministic,
snapshot-friendly multi-line string.
public static string Render(FakeLogCollector collector, LogSnapshotOptions? options = null)
Parameters
collectorFakeLogCollectorThe fake collector whose captured records to render.
optionsLogSnapshotOptions
Returns
- string
A multi-line string with one block per captured record, or Empty when the collector has captured nothing.
Exceptions
- ArgumentNullException
collectoris null.