LogAssertions 0.6.0

Prefix Reserved
dotnet add package LogAssertions --version 0.6.0
                    
NuGet\Install-Package LogAssertions -Version 0.6.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LogAssertions" Version="0.6.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LogAssertions" Version="0.6.0" />
                    
Directory.Packages.props
<PackageReference Include="LogAssertions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LogAssertions --version 0.6.0
                    
#r "nuget: LogAssertions, 0.6.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LogAssertions@0.6.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LogAssertions&version=0.6.0
                    
Install as a Cake Addin
#tool nuget:?package=LogAssertions&version=0.6.0
                    
Install as a Cake Tool

LogAssertions

NuGet License: MIT .NET

Scope: Test projects only. Not intended for production code.

Framework-agnostic core for fluent log assertions over Microsoft.Extensions.Logging.Testing.FakeLogCollector.

Most users want LogAssertions.TUnit, not this package directly. This is the shared engine; framework-specific adapter packages add the assertion entry points your test framework expects.


What's in this package

  • ILogRecordFilter: composable filter interface (Matches(FakeLogRecord) + Description).
  • LogFilter: static factory for every built-in filter shape (AtLevel, Containing, WithMessageTemplate, WithException, WithoutException (v0.6.0+), WithInnerException (v0.4.0+), WithInnerExceptionMessage (v0.4.0+), WithProperty, WithProperty<T> (v0.6.0+), WithCategory, WithEventId, WithScope, WithScopeProperty, WithScopeProperty<T> (v0.6.0+), WithScopeProperties (v0.4.0+), Where, etc.) plus combinators (All, Any, Not).
  • LogAssertionRendering: failure-snapshot rendering (4-character level abbreviation, props line, scopes line, exception line) with DumpVerbosity-controlled detail level (v0.4.0+: Compact / Default / Verbose). Used by adapter packages and by the DumpTo extension below.
  • LogCollectorBuilder.Create(LogLevel): one-line factory returning a wired (ILoggerFactory, FakeLogCollector) tuple.
  • Inspection extensions on FakeLogCollector: Filter(params ILogRecordFilter[]) returns matching records, CountMatching(params ILogRecordFilter[]) returns the count, DumpTo(TextWriter) and DumpTo(TextWriter, DumpVerbosity) (v0.4.0+) write the captured-records snapshot.

Test-framework adapters

Package Test framework Status
LogAssertions.TUnit TUnit Available now
LogAssertions.NUnit NUnit Possible if there is demand
LogAssertions.xUnit xUnit Possible if there is demand
LogAssertions.MSTest MSTest Possible if there is demand

If you'd find a non-TUnit adapter useful, open a feature request: adapters are not built proactively.

Installation

dotnet add package LogAssertions.TUnit

LogAssertions comes transitively. You don't need to install it directly unless you're building your own adapter package.

Direct use

If you're building a custom adapter or want non-asserting access to filtered records, the public surface lets you compose filters and inspect the collector without going through any test-framework's assertion type:

using LogAssertions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;

var (factory, collector) = LogCollectorBuilder.Create();
using (factory)
{
    var logger = factory.CreateLogger("MyCategory");
    logger.LogWarning("validation failed: TimeoutMs out of range");

    // Compose a reusable filter:
    ILogRecordFilter validationWarning = LogFilter.All(
        LogFilter.AtLevel(LogLevel.Warning),
        LogFilter.Containing("validation", StringComparison.Ordinal));

    // Inspect without asserting:
    int count = collector.CountMatching(validationWarning);                  // 1
    var matches = collector.Filter(validationWarning);                       // IReadOnlyList<FakeLogRecord>
    using var writer = new StringWriter();
    collector.DumpTo(writer);                                                // print the captured-records snapshot
}

Building a custom adapter

A test-framework adapter package needs to:

  1. Reference LogAssertions.
  2. Provide entry-point methods that surface a fluent chain over the framework's assertion type.
  3. Use LogFilter factory methods to build the filter chain.
  4. Use LogAssertionRendering.AppendCapturedRecords(...) for failure-message snapshot output (so the format stays consistent across adapters).

LogAssertions.TUnit is the reference implementation: see its source for the pattern.

Stability

  • The public surfaces above (ILogRecordFilter, LogFilter, LogAssertionRendering, LogCollectorBuilder, the extension methods on FakeLogCollector) are semver-bound. Breaking changes require a major version bump.
  • The exact text format of failure-message snapshots rendered by LogAssertionRendering is not stable. May gain extra detail or change formatting in any release. Pin filter match counts and broad markers in tests, not exact failure text.

License

MIT. Copyright (c) 2026 John Verheij.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on LogAssertions:

Package Downloads
LogAssertions.TUnit

TUnit-native fluent log-assertion DSL using [AssertionExtension] over Microsoft.Extensions.Logging.Testing.FakeLogCollector. Provides Assert.That(collector).HasLogged()/HasNotLogged() with filter and terminator chaining; emits captured-records snapshot in failure messages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.6.0 0 6/2/2026
0.5.0 175 5/14/2026
0.4.1 308 5/12/2026
0.4.0 153 5/7/2026
0.3.0 183 5/2/2026
0.2.4 142 5/2/2026
0.2.3 116 5/1/2026
0.2.2 109 5/1/2026
0.2.1 127 5/1/2026
0.2.0 105 5/1/2026

View the rendered release notes: https://github.com/JohnVerheij/LogAssertions.TUnit/releases/tag/v0.6.0

### BREAKING

- The single-arg `WithExceptionMessage(string substring)` overload (on `LogFilter` and on the `HasLoggedAssertion` / `HasNotLoggedAssertion` / `HasLoggedSequenceAssertion` chain), `[Obsolete]` since v0.4.0, has been removed. Append `, StringComparison.Ordinal` to existing call sites to keep the previous behavior, or pass a different comparison for case-insensitive / culture-aware matching. See `### Removed` below.

### Added

- **`LogFilter.WithoutException()`** and the matching **`WithoutException()`** chain method on `HasLoggedAssertion` / `HasNotLoggedAssertion` / `HasLoggedSequenceAssertion`: matched records whose `Exception` is `null`. The complement of `WithException()`. Lets a test assert the deliberate absence of an exception (for example a record logged at warning or error level with no exception object attached) without the `.Where(r => r.Exception is null)` escape hatch.
- **`LogFilter.WithScopeProperty<T>(string key, T value)`** and **`LogFilter.WithScopeProperty<T>(string key, Func<T, bool> predicate)`** plus the matching chain methods: typed scope-property filters. Scope values keep their runtime type, so the value form compares typed-to-typed via `EqualityComparer<T>.Default` and the predicate form receives the typed value; a scope value whose runtime type is not `T` never matches. Removes the object-boxing boilerplate of the existing object-typed overloads when the scope value is a known type such as `Guid` or `int`.
- **`LogFilter.WithProperty<T>(string key, T value)`** and **`LogFilter.WithProperty<T>(string key, Func<T, bool> predicate)`** (`where T : IParsable<T>`) plus the matching chain methods: typed structured-state filters. `FakeLogRecord` stores structured-state values as their formatted strings, so the stored string is parsed back to `T` via `IParsable<T>.TryParse` using `CultureInfo.InvariantCulture` before comparing (value form) or applying the predicate; an absent or non-parsable value never matches. Removes the manual `int.TryParse(...)` boilerplate at the call site. The `IParsable<T>` constraint keeps the round-trip reflection-free and AOT-safe.

### Changed

- **`TUnit`** / **`TUnit.Assertions`** / **`TUnit.Core`** dependency bumped `1.44.0` → `1.48.6`. Taken for family lockstep; the sibling adapters are on the same pin.

### Removed

- **`LogFilter.WithExceptionMessage(string substring)`** and the matching single-arg `WithExceptionMessage(string)` chain method on `HasLoggedAssertion` / `HasNotLoggedAssertion` / `HasLoggedSequenceAssertion`: the implicit-`Ordinal` overloads, `[Obsolete]` since v0.4.0, are gone. The explicit-comparison overload `WithExceptionMessage(string substring, StringComparison comparison)` is unchanged. Migrate by appending `, StringComparison.Ordinal` to existing call sites. Removing a public member is a source-and-binary breaking change; see `### BREAKING` above.