Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Last active May 13, 2025 11:01
Show Gist options
  • Save dj-nitehawk/58c14fd593cf58fa5e8df95cfb9eb549 to your computer and use it in GitHub Desktop.
Save dj-nitehawk/58c14fd593cf58fa5e8df95cfb9eb549 to your computer and use it in GitHub Desktop.
Forwarding app log messages to xUnit message sink
{
"diagnosticMessages": true
}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="MartinCostello.Logging.XUnit.v3" Version="*"/>
</ItemGroup>
<ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="Always"/>
</ItemGroup>
</Project>
public class App(IMessageSink sink) : AppFixture<Program>
{
protected override void ConfigureApp(IWebHostBuilder a)
{
a.ConfigureLogging(b => b.ClearProviders().AddXUnit(sink));
}
}
  • create a xunit.runner.json file at project root and enable diagnostic messages
  • add a package reference to MartinCostello.Logging.XUnit.v3 in the test project
  • inject a IMessageSink into the app fixture and configure logging with the sink
  • run dotnet test --logger "console;verbosity=normal" to see log messages

Click here for the full sample project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment