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

Agoda.NUnit.KestrelLogging

License NuGet

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:

  1. Creating a unique token for each test instance
  2. Storing the NUnit test context associated with that token
  3. Adding middleware that restores the test context for each HTTP request
  4. 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 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. 
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
1.0.13 40 10/26/2025