Skip to content

Clarify how to capture logs from tests #2096

@afscrome

Description

@afscrome

Type of issue

Missing information

Description

The docs don't mention how to get logs out of tests. There is a very brief comment that shows up in the xunit code sample, but it doesn't show for other frameworks

        // To output logs to the xUnit.net ITestOutputHelper, 
        // consider adding a package from https://www.nuget.org/packages?q=xunit+logging

It took me quite a while to work out that I was meant to use the AddLogging method on the services, and even longer to work out I needed to configure some default log filters as the appsettings.json from the application under test wasn't being replicated into the test projects.

      appHost.Services.AddLogging(x => x
            .AddNUnit() // https://www.nuget.org/packages/Extensions.Logging.NUnit
            .AddFilter("Default", LogLevel.Information)
            .AddFilter("Microsoft.AspNetCore", LogLevel.Warning)
            .AddFilter("Aspire.Hosting.Dcp", LogLevel.Warning)
      );

Page URL

https://learn.microsoft.com/en-us/dotnet/aspire/testing/write-your-first-test

Content source URL

https://github.com/dotnet/docs-aspire/blob/main/docs/testing/write-your-first-test.md

Document Version Independent Id

493680b6-598d-1d8c-6e0e-f54286ae77e0

Article author

@IEvangelist

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions