StubLogger 2.0.0.6
dotnet add package StubLogger --version 2.0.0.6
NuGet\Install-Package StubLogger -Version 2.0.0.6
<PackageReference Include="StubLogger" Version="2.0.0.6" />
<PackageVersion Include="StubLogger" Version="2.0.0.6" />
<PackageReference Include="StubLogger" />
paket add StubLogger --version 2.0.0.6
#r "nuget: StubLogger, 2.0.0.6"
#:package StubLogger@2.0.0.6
#addin nuget:?package=StubLogger&version=2.0.0.6
#tool nuget:?package=StubLogger&version=2.0.0.6
StubLogger
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 eventsassertion: 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 eventsexpectedCount: 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 messageException: 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 | Versions 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. |
-
net10.0
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.2)
- xunit.assert (>= 2.9.3)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.2)
- xunit.assert (>= 2.9.3)
-
net9.0
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.2)
- xunit.assert (>= 2.9.3)
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 |