Agoda.NUnit.KestrelLogging
1.0.13
Prefix Reserved
dotnet add package Agoda.NUnit.KestrelLogging --version 1.0.13
NuGet\Install-Package Agoda.NUnit.KestrelLogging -Version 1.0.13
<PackageReference Include="Agoda.NUnit.KestrelLogging" Version="1.0.13" />
<PackageVersion Include="Agoda.NUnit.KestrelLogging" Version="1.0.13" />
<PackageReference Include="Agoda.NUnit.KestrelLogging" />
paket add Agoda.NUnit.KestrelLogging --version 1.0.13
#r "nuget: Agoda.NUnit.KestrelLogging, 1.0.13"
#:package Agoda.NUnit.KestrelLogging@1.0.13
#addin nuget:?package=Agoda.NUnit.KestrelLogging&version=1.0.13
#tool nuget:?package=Agoda.NUnit.KestrelLogging&version=1.0.13
Agoda.NUnit.KestrelLogging
A helper library for NUnit tests with ASP.NET Core Kestrel that fixes Console.WriteLine and TestContext output not appearing in test results.
The Problem
When testing ASP.NET Core applications with NUnit and WebApplicationFactory, output from Console.WriteLine and TestContext.WriteLine inside HTTP request handlers doesn't appear in test results. This is because NUnit's TestExecutionContext is stored in an AsyncLocal<T> that gets lost when Kestrel handles requests on different threads.
Issue Reference: nunit/nunit#4860
Example of the Problem
[Test]
public async Task TestEndpoint()
{
await using var factory = new WebApplicationFactory<Program>();
var client = factory.CreateClient();
var response = await client.GetAsync("/");
// Console.WriteLine from inside the endpoint handler won't appear! ❌
}
The Solution
This library provides a simple extension method that restores the NUnit test context for all HTTP requests, ensuring that all output appears in your test results.
Installation
dotnet add package Agoda.NUnit.KestrelLogging
Usage
Simply call .WithNUnitTestLogging(out var scope) on your WebApplicationFactory and dispose the scope when your test completes:
using Agoda.NUnit.KestrelLogging;
using Microsoft.AspNetCore.Mvc.Testing;
using NUnit.Framework;
[Test]
public async Task TestEndpoint()
{
// Create factory with NUnit logging support
await using var factory = new WebApplicationFactory<Program>()
.WithNUnitTestLogging(out var scope);
using (scope)
{
var client = factory.CreateClient();
var response = await client.GetAsync("/");
// Console.WriteLine from inside the endpoint handler will now appear! ✅
response.EnsureSuccessStatusCode();
}
}
Your Endpoint Code
app.MapGet("/", () =>
{
Console.WriteLine("This will now appear in test output!");
TestContext.WriteLine("This works too!");
return "Hello World!";
});
How It Works
The library solves the problem by:
- Creating a unique token for each test instance
- Storing the NUnit test context associated with that token
- Adding middleware that restores the test context for each HTTP request
- Configuring logging to write to NUnit's test output
This ensures that the AsyncLocal<TestExecutionContext> is properly restored when Kestrel handles requests, making all output appear in test results.
Requirements
- .NET 8.0 or later
- NUnit 4.0 or later
- Microsoft.AspNetCore.Mvc.Testing 8.0 or later
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Made with ❤️ by Agoda
| 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 was computed. 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 was computed. 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. |
-
net8.0
- Microsoft.AspNetCore.Mvc.Testing (>= 8.0.0)
- NUnit (>= 4.4.0)
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 |
|---|---|---|
| 1.0.13 | 40 | 10/26/2025 |