feat: add TUnit.Logging.Microsoft package and shared web app logging#4817
Merged
feat: add TUnit.Logging.Microsoft package and shared web app logging#4817
Conversation
Create a new TUnit.Logging.Microsoft package that provides Microsoft.Extensions.Logging integration without requiring ASP.NET Core dependencies. This allows users who just need IHost or generic DI to capture app-level ILogger output in TUnit test output. For shared WebApplicationFactory scenarios, add correlated logging support to TUnit.AspNetCore via TUnitTestIdHandler (client-side header propagation) and TUnitTestContextMiddleware + CorrelatedTUnitLoggerProvider (server-side context resolution per log call). Key improvements over the previous TUnitAspNetLogger: - Writes directly to TestContext.Output instead of Console.WriteLine - Error-level logs routed to WriteError() for proper separation - No longer sets TestContext.Current as a side effect in Log() Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. This PR introduces a well-structured logging abstraction:
The approach of separating generic |
- Add TUnit.Logging.Microsoft to project-structure.md - Rewrite TUnit Logging Integration section in aspnet.md with: - Per-test logging with isolated factory - Standalone logging without ASP.NET Core - Shared web app logging with header propagation - Add new types and extensions to API reference Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Set TestContext.Current before writing via Console.WriteLine/Console.Error.WriteLine so the console interceptor and all registered log sinks (TestOutputSink, ConsoleOutputSink, RealTimeOutputSink) naturally pick up and route the output. This matches the original TUnitAspNetLogger pattern and ensures IDE real-time output works correctly. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 task
Automatically register correlated logging in CreateHostBuilder() and per-test logging in GetIsolatedFactory() so users get logging out of the box without manual setup. Add TUnitLoggingRegistry for duplicate prevention when both loggers are active. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Update ASP.NET Core and logging docs to reflect that TestWebApplicationFactory now auto-registers both per-test and correlated logging. Users no longer need manual setup — just use CreateClientWithTestContext() for shared factory scenarios. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The CI was failing because TUnit.AspNetCore now depends on TUnit.Logging.Microsoft, but the new package wasn't being packed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Feb 16, 2026
Chore(deps): Bump TUnit.Assertions from 1.12.111 to 1.15.0
code-of-chaos/cs_code-of_chaos-testing#91
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TUnit.Logging.Microsoftpackage — providesMicrosoft.Extensions.Loggingintegration without ASP.NET Core dependencies, enabling ILogger output capture in TUnit test output for anyIHostor generic DI scenarioWebApplicationFactorylogging — addsTUnitTestIdHandler(client-side header propagation) andCorrelatedTUnitLoggerProvider+TUnitTestContextMiddleware(server-side per-request test context resolution) so a single shared web app can route logs to the correct testTestContext.Output.WriteLine()/WriteError()instead ofConsole.WriteLine, and error-level logs are properly separatedNew package:
TUnit.Logging.MicrosoftTUnitLogger/TUnitLoggerProvider— writes directly to test context outputILoggingBuilder.AddTUnit(context)andIServiceCollection.AddTUnitLogging(context)extension methodsIHost-based appShared web app support in
TUnit.AspNetCoreTUnitTestIdHandler—DelegatingHandlerthat propagatesTestContext.IdviaX-TUnit-TestIdheaderTUnitTestContextMiddleware— extracts test ID from request headers and resolvesTestContextviaGetById()CorrelatedTUnitLogger— resolves test context per log call (HttpContext items → AsyncLocal fallback → no-op)IServiceCollection.AddCorrelatedTUnitLogging()— one-call registration withIStartupFilterWebApplicationFactory<T>.CreateClientWithTestContext()— convenience extensionBreaking changes
TUnitAspNetLogger,TUnitLoggerProvider,TUnitLoggerScoperemoved fromTUnit.AspNetCore.Loggingnamespace (replaced byTUnit.Logging.Microsofttypes)AddTUnit,AddTUnitLogging) remain API-compatible inTUnit.AspNetCore.ExtensionsTest plan
dotnet build TUnit.Logging.Microsoft— all 3 TFMs (net8.0/net9.0/net10.0) compile cleanlydotnet build TUnit.AspNetCore— all 3 TFMs compile cleanlydotnet test TUnit.AspNetCore.Analyzers.Tests— net8.0/net9.0 pass (net10.0 failures are pre-existing System.Runtime version mismatches)AddTUnitLogging(testContext)captures ILogger output in test resultsAddCorrelatedTUnitLogging()+CreateClientWithTestContext()routes shared app logs to correct test🤖 Generated with Claude Code