StubLogger 2.0.0.6

dotnet add package StubLogger --version 2.0.0.6
                    
NuGet\Install-Package StubLogger -Version 2.0.0.6
                    
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="StubLogger" Version="2.0.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StubLogger" Version="2.0.0.6" />
                    
Directory.Packages.props
<PackageReference Include="StubLogger" />
                    
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 StubLogger --version 2.0.0.6
                    
#r "nuget: StubLogger, 2.0.0.6"
                    
#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 StubLogger@2.0.0.6
                    
#: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=StubLogger&version=2.0.0.6
                    
Install as a Cake Addin
#tool nuget:?package=StubLogger&version=2.0.0.6
                    
Install as a Cake Tool

StubLogger

NuGet

A lightweight stub logger for unit testing with xUnit that tracks and asserts log events in .NET applications.

Overview

StubLogger provides a simple and intuitive way to verify logging behaviour in your unit tests. It implements ILogger<T> and tracks all log events, allowing you to assert that specific log messages were written with the correct log levels and exceptions.

Installation

Install the StubLogger NuGet package:

dotnet add package StubLogger

Or via the Package Manager Console:

Install-Package StubLogger

Usage

Basic Usage

Create a StubLogger<T> instance in your test and inject it into the class under test:

using Microsoft.Extensions.Logging;
using StubLogger;
using Xunit;

public class MyServiceTests
{
    private readonly StubLogger<MyService> _logger = new();
    private readonly MyService _sut;

    public MyServiceTests()
    {
        _sut = new MyService(_logger);
    }

    [Fact]
    public void ProcessData_WhenCalled_LogsInformation()
    {
        // arrange & act
        _sut.ProcessData();

        // assert
        _logger.AssertLogEvent(e => 
            e.LogLevel == LogLevel.Information && 
            e.Message.Contains("Processing data"));
    }
}

Asserting Log Events

Use AssertLogEvent to verify that a specific log event was written:

[Fact]
public void Method_WhenWarningCondition_LogsWarning()
{
    // arrange & act
    _sut.MethodWithWarning();

    // assert
    _logger.AssertLogEvent(e => 
        e.LogLevel == LogLevel.Warning && 
        e.Message.Contains("Expected warning message"));
}

Asserting Log Event Count

Use AssertLogCount to verify the number of log events that match a predicate:

[Fact]
public void Method_WhenCalledMultipleTimes_LogsCorrectCount()
{
    // arrange & act
    _sut.ProcessItem("Item1");
    _sut.ProcessItem("Item2");
    _sut.ProcessItem("Item3");

    // assert
    _logger.AssertLogCount(e => 
        e.LogLevel == LogLevel.Information, 3);
}

Asserting Exceptions

Verify that exceptions are logged correctly:

[Fact]
public void Method_WhenExceptionOccurs_LogsError()
{
    // arrange
    var exception = new InvalidOperationException("Invalid operation");

    // act
    _sut.HandleError(exception);

    // assert
    _logger.AssertLogEvent(e => 
        e.LogLevel == LogLevel.Error && 
        e.Message.Contains("An error occurred") && 
        e.Exception == exception);
}

Advanced Exception Assertions

You can use a separate assertion action to verify exception details:

[Fact]
public void Method_WhenExceptionOccurs_LogsErrorWithCorrectExceptionType()
{
    // arrange
    var exception = new InvalidOperationException("Invalid operation");

    // act
    _sut.HandleError(exception);

    // assert
    _logger.AssertLogEvent(
        e => e.LogLevel == LogLevel.Error && 
             e.Message.Contains("An error occurred"),
        e => e.Exception.Should().BeOfType<InvalidOperationException>());
}

API Reference

StubLogger<T>

Implements ILogger<T> and provides assertion methods for unit testing.

Methods
  • AssertLogEvent(Func<TrackedLogEvent, bool> predicate, Action<TrackedLogEvent>? assertion = null)

    Asserts that a log event matching the given predicate exists. Optionally performs additional assertions on the matched log event.

    • predicate: A function to match log events
    • assertion: An optional action to perform additional assertions
  • AssertLogCount(Func<TrackedLogEvent, bool> predicate, int expectedCount)

    Asserts that the count of log events matching the given predicate equals the expected count.

    • predicate: A function to match log events
    • expectedCount: The expected number of matching log events

TrackedLogEvent

Represents a tracked log event with the following properties:

  • LogLevel: The log level of the event (Information, Warning, Error, etc.)
  • Message: The formatted log message
  • Exception: The optional exception associated with the log event (null if no exception)

Requirements

  • .NET 8.0, 9.0, or 10.0
  • xUnit 2.9.3 or higher
  • Microsoft.Extensions.Logging.Abstractions 10.0.0 or higher

License

This project is licensed under Apache License 2.0.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0.6 51 2/8/2026
1.1.2.23-beta 46 2/8/2026
1.1.2.22-beta 45 2/8/2026
1.1.2.7 49 2/8/2026
1.1.1.5 53 2/8/2026
1.1.0.21-beta 48 2/8/2026
1.1.0.20-beta 48 2/8/2026
1.1.0.19-beta 45 2/8/2026
1.1.0.18-beta 130 12/26/2025
1.1.0.17-beta 170 12/22/2025
1.1.0.16-beta 150 12/21/2025
1.1.0.15-beta 150 12/21/2025
1.1.0.14-beta 149 12/21/2025
1.1.0.4 281 12/17/2025
1.0.1.13-beta 263 12/17/2025
1.0.1.12-beta 268 12/17/2025
1.0.1.11-beta 269 12/17/2025
1.0.1.10-beta 259 12/17/2025
1.0.1.9-beta 262 12/17/2025
1.0.1.8-beta 258 12/17/2025
1.0.1.7-beta 265 12/17/2025
1.0.1.3 132 12/6/2025
1.0.0.6-beta 114 12/6/2025
1.0.0.3-beta 118 12/6/2025
1.0.0.2 115 12/6/2025
0.0.0.2-beta 120 12/6/2025
0.0.0.1-beta 129 12/6/2025