0

I am having an issue getting logs that are written to the logger during an test runs to actually be written out to the console unless the corresponding WebApplicationFactory is created and disposed between tests runs...which isn't really that optimal :/

We have an a bunch of api integration tests, the tests conceptual all have the following pattern (not structure).


[TestFixture]
public class MyControllerTest 
{ 
    CustomWebApplicationFactory _factory;
    HttpClient _client;

    public OneTimeSetup() 
    {
        _factory = new CustomWebApplicationFactory();
    }

    public SetUp() 
    { 
       _client = _factory.CreateClient();
    }

    public async Task TestA()  
    {
        /* Some Test Using the _client */
    }

    public async Task TestA()  
    {
       /* Some Test Using the _client */
    }

    public TearDown() 
    {
       _client.Dispose();
    }

    public OneTimeTearDown() 
    {
        _factory.Dispose();
    }

}

Nothing inherently peculiar.

The CustomWebApplicationFactory above looks something like:


public class CustomWebApplicationFactory : WebApplicationFactory<Startup>
{
   /* Bunch of stuff removed for brevity */
   protected override void ConfigureWebHost(IWebHostBuilder builder)
   {

        base.ConfigureWebHost(builder);
        builder.ConfigureTestServices(s => { /* Some general mocked services */ });
           
        builder
            .ConfigureLogging(loggingBuilder =>
            {
                loggingBuilder
                    .ClearProviders()
                    .AddConsole()
                    .AddDebug()
                    .AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Information)
                    .SetMinimumLevel(LogLevel.Information);
            });
    }
}

The issue I am having is particularly to do with the ef core DbContext query logging. These logs are not written to the console if the factory is not disposed between test runs.
So in short I was able to get the behaviour I wanted by moving the moving the _factory initialization and disposing too the SetUp and TearDown functions respectively. But this seems to introduce a bit of fun issue on the testing servers...and also I don't really want to create and dispose the factory for every Test regardless.
Has anyone had a similar scenario? Is there away to flush the logs? instead of disposing the factory everyime?

Heinrich
  • 2,144
  • 3
  • 23
  • 39

0 Answers0