Skip to content

WebApplicationFactory logging regression with v3.10.0 #6457

Closed as not planned

Description

@markm77

Hi there,

I have a .NET 9 test project (<Project Sdk="MSTest.Sdk/3.x.y">) which uses a WebApplicationFactory (set up as a fixture in [ClassInitialize]). Logging from the WebApplicationFactory has stopped and I isolated the issue to updating from MSTest v3.9.3 to v3.10.0. Reverting back to v3.9.3 has fixed things.

I have quite a complicated project so before setting out to create a minimal repro I just wanted to ask if there are any obvious reasons for this (I couldn't see anything in the release log).

BR,
Mark

Activity

  1. Youssef1313 commented on Aug 28, 2025

    @Youssef1313
    Member

    Unfortunately, it's hard to say/guess without a repro. If you can create a minimal repro, that's very much appreciated.

  2. added
    needs/info
    type/regression
    area/fixtures
    area/mstest
    and removed
    needs/triage
    on Aug 28, 2025
  3. markm77 commented on Aug 28, 2025

    @markm77
    Author

    OK, I'll have a go at this next week. Thanks!

  4. Youssef1313 commented on Sep 3, 2025

    @Youssef1313
    Member

    Mark Murphy (@markm77) Were you able to get a minimal repro please? Thanks!

  5. markm77 commented on Sep 7, 2025

    @markm77
    Author

    Sorry Youssef Fahmy (@Youssef1313) , will try to do this tomorrow!

  6. markm77 commented on Sep 8, 2025

    @markm77
    Author

    Still working on this....

  7. markm77 commented on Sep 9, 2025

    @markm77
    Author

    Hi Youssef Fahmy (@Youssef1313) ,

    OK I've got to the bottom of what is happening. This was tough to repro as everything seemed to work in simple cases. However let me go through it.

    If I have a simple web app:

    var builder = WebApplication.CreateBuilder(args);
    
    var app = builder.Build();
    
    app.MapGet("/", (ILogger<Program> logger) =>
    {
        logger.LogWarning("Log message from endpoint");
        return "Endpoint alive";
    });
    
    app.Run();
    
    public partial class Program { }

    and test with an <Project Sdk="MSTest.Sdk/3.9.3"> project test:

    using Microsoft.AspNetCore.Mvc.Testing;
    
    namespace Tests;
    
    [TestClass]
    public sealed class MyTestClass
    {
        private static WebApplicationFactory<Program> _webApplicationFactory = null!;
    
        [TestMethod]
        public async Task TestMethod()
        {
            Console.WriteLine("TestMethod: start");
            using var httpClient = _webApplicationFactory.CreateClient();
            var response = await httpClient.GetStringAsync("/");
            Console.WriteLine("TestMethod: finish");
        }
    
        [ClassInitialize]
        public static void ClassInitialise(TestContext context)
        {
            Console.WriteLine("ClassInitialise: start");
            _webApplicationFactory = new WebApplicationFactory<Program>();
            using var httpClient = _webApplicationFactory.CreateClient(); // When commented out, no problems
            Console.WriteLine("ClassInitialise: finish");
        }
    
        [ClassCleanup]
        public static void ClassCleanup()
        {
            _webApplicationFactory.Dispose();
        }
    }

    I get test output:

    ClassInitialise: start
    ClassInitialise: finish
    TestMethod: start
    TestMethod: finish
    2025-09-09T12:20:43.598Z warn: Program[0]
          => SpanId:9a9d2bc51064edd2, TraceId:d5a26339614df42dd1d8a07e7bc9e041, ParentId:0000000000000000 => RequestPath:/ RequestId:0HNFFJD64SNU7
          Log message from endpoint
    

    And if I use <Project Sdk="MSTest.Sdk/3.10.3"> I get test output

    ClassInitialise: start
    ClassInitialise: finish
    TestMethod: start
    TestMethod: finish
    

    where everything after TestMethod: finish is removed.

    I have noted by a comment which line in ClassInitialise() causes logging to occur after TestMethod: finish but actually such "delayed logging" also happens in my web app - this is just an example to demonstrate the issue.

    Hope this helps clear up what is happening.

  8. Youssef1313 commented on Sep 9, 2025

    @Youssef1313
    Member

    Thanks Mark Murphy (@markm77)

    Can you please zip the whole project/solution?

  9. Youssef1313 commented on Sep 23, 2025

    @Youssef1313
    Member

    Investigated more. The ConsoleLoggerProcessor is initiated the first time _webApplicationFactory.CreateClient() is called through this stack trace:

    Image

    CreateHost then creates a thread and starts it (which flows the execution context).
    When the execution context that flows is from class initialize, we don't have the correct test context to write to. Note: The created thread here is what calls the entry point of the web app, and is where the logging is setup.

    So from MSTest perspective, this is working as intended.

    Also note that with MSTest 3.9.3, the behavior was actually wrong. If you add more test methods to the class, you will find all logging is attached to first one, which is obviously wrong and confusing.

  10. added
    external/other
    resolution/by-design
    on Sep 23, 2025
  11. markm77 commented on Sep 24, 2025

    @markm77
    Author

    Hi Youssef Fahmy (@Youssef1313) , Sorry to not get back to you on this. I was a bit concerned about leaking proprietary info in a zip and then got caught up in a whirlwind of other things. I am a little bit struggling to understand your explanation but I think you are saying CreateHost is being called from ClassInitialize rather than TestMethod and MSTest will no longer accept console output from such an execution context?

    Is the solution to only call CreateHost from TestMethod?

  12. Youssef1313 commented on Sep 24, 2025

    @Youssef1313
    Member

    Yes, it's by-design that logging should happen to the correct TestContext (which is internally an async local and is tied to the execution context where the logging is happening).

    I suggest reaching out to aspnetcore to suggest if they have already an advise how to set up tests correctly.

  13. markm77 commented on Sep 24, 2025

    @markm77
    Author

    If I understand what you are saying correctly it seems the first call to CreateClient() creates the execution context for all future console logging? And each TestContext will only accept console logging from the correct execution context?

    Aspnetcore already have documentation on how to set up MSTest tests: https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-9.0&pivots=mstest#basic-tests-with-the-default-webapplicationfactory. I will test this and see if logging works correctly.

  14. Youssef1313 commented on Sep 24, 2025

    @Youssef1313
    Member

    If I understand what you are saying correctly it seems the first call to CreateClient() creates the execution context for all future console logging? And each TestContext will only accept console logging from the correct execution context?

    It's not that CreateClient creates execution context. It's more of "CreateClient is called on the execution context of the class init which has the current test context as the one of class initialize, so we don't have any way to associate logs to the right test".

    It might still be better to reach out to aspnetcore team, as the currently documented approach doesn't sound ideal.

    FYI Brennan (@BrennanConroy) Safia Abdalla (@captainsafia)

  15. added a commit that references this issue on Nov 8, 2025
  16. added a commit that references this issue on Nov 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/fixturesarea/mstestexternal/otherneeds/inforesolution/by-designtype/regression

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions