ConnectingApps.Xunit.TestLogger 1.1.0

dotnet add package ConnectingApps.Xunit.TestLogger --version 1.1.0
NuGet\Install-Package ConnectingApps.Xunit.TestLogger -Version 1.1.0
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="ConnectingApps.Xunit.TestLogger" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ConnectingApps.Xunit.TestLogger --version 1.1.0
#r "nuget: ConnectingApps.Xunit.TestLogger, 1.1.0"
#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.
// Install ConnectingApps.Xunit.TestLogger as a Cake Addin
#addin nuget:?package=ConnectingApps.Xunit.TestLogger&version=1.1.0

// Install ConnectingApps.Xunit.TestLogger as a Cake Tool
#tool nuget:?package=ConnectingApps.Xunit.TestLogger&version=1.1.0

Xunit.TestLogger

Before this NuGet package

If you make integration tests with xUnit, your logging is gone. You can just see if the test succeeds or fails but you cannot see why as you don't have the logging to help you understanding what happened during the runtime of the test, which makes it hard to solve it.

Now

The is situation is different now! The problem is solved for .NET 6, .NET 7 and .NET 8. Alle you need to do is stop using this way of creating a WebApplicationFactory:

public NoLoggingTest()
{
    _factory = new WebApplicationFactory<Program>();
}

and start creating it this way

public ImprovedLoggingTest(ITestOutputHelper output)
{
    _factory = new TestLoggerWebApplicationFactory<Program>(output);
}

Here is how it looks like:

Link To ScreenShot Alt text

In Detail

Assume you have the following controller method (just the method from the template in Visual Studio with some logging added):

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
    // first do some logging
    _logger.LogInformation("This should be logged during testing");
    // Then return an object like it is done in the VS Template
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateTime.Now.AddDays(index),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .ToArray();
}

then you can test it like this:

using ConnectingApps.Xunit.TestLogger;
using System.Net;
using Xunit.Abstractions;

namespace ConnectingApps.LoggingWebApi.IntegrationTest
{
    public class ImprovedLoggingTest : IDisposable
    {
        private readonly TestLoggerWebApplicationFactory<Program> _factory;
        private readonly HttpClient _client;


        public ImprovedLoggingTest(ITestOutputHelper output)
        {
            _factory = new TestLoggerWebApplicationFactory<Program>(output);
            _client = _factory.CreateClient();
        }

        [Fact]
        public async Task ReadInTestOutputIfSomethingIsLogged()
        {
            var response = await _client.GetAsync("/WeatherForecast");
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }

        public void Dispose()
        {
            _factory.Dispose();
            _client.Dispose();
        }
    }
}

As a result, "This should be logged during testing" will be shown in the test output.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
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.1.0 425 11/14/2023
1.0.3 93 10/19/2023
1.0.2-preview 55 10/19/2023
1.0.1-preview 99 10/18/2023
1.0.0-preview 97 10/18/2023

Full support for .NET 8 (no rc or preview)