You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
WebApplicationFactory logging regression with v3.10.0 #6457
area/fixturesTest class / assembly initialization & cleanup.area/mstestMSTest framework (not analyzers or assertions).external/otherCaused by an external issue that needs to be solved first.needs/infoNeeds more information from the reporter.resolution/by-designClosed: behavior is by design.type/regressionRegression from a previous release.
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).
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:
varbuilder=WebApplication.CreateBuilder(args);varapp=builder.Build();app.MapGet("/",(ILogger<Program>logger)=>{logger.LogWarning("Log message from endpoint");return"Endpoint alive";});app.Run();publicpartialclassProgram{}
and test with an <Project Sdk="MSTest.Sdk/3.9.3"> project test:
usingMicrosoft.AspNetCore.Mvc.Testing;namespaceTests;[TestClass]publicsealedclassMyTestClass{privatestaticWebApplicationFactory<Program>_webApplicationFactory=null!;[TestMethod]publicasyncTaskTestMethod(){Console.WriteLine("TestMethod: start");usingvarhttpClient=_webApplicationFactory.CreateClient();varresponse=awaithttpClient.GetStringAsync("/");Console.WriteLine("TestMethod: finish");}[ClassInitialize]publicstaticvoidClassInitialise(TestContextcontext){Console.WriteLine("ClassInitialise: start");_webApplicationFactory=newWebApplicationFactory<Program>();usingvarhttpClient=_webApplicationFactory.CreateClient();// When commented out, no problemsConsole.WriteLine("ClassInitialise: finish");}[ClassCleanup]publicstaticvoidClassCleanup(){_webApplicationFactory.Dispose();}}
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.
Investigated more. The ConsoleLoggerProcessor is initiated the first time _webApplicationFactory.CreateClient() is called through this stack trace:
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.
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?
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.
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?
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 CreateClientcreates 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.
area/fixturesTest class / assembly initialization & cleanup.area/mstestMSTest framework (not analyzers or assertions).external/otherCaused by an external issue that needs to be solved first.needs/infoNeeds more information from the reporter.resolution/by-designClosed: behavior is by design.type/regressionRegression from a previous release.
Activity
Youssef1313 commentedon Aug 28, 2025
Unfortunately, it's hard to say/guess without a repro. If you can create a minimal repro, that's very much appreciated.
markm77 commentedon Aug 28, 2025
OK, I'll have a go at this next week. Thanks!
Youssef1313 commentedon Sep 3, 2025
Mark Murphy (@markm77) Were you able to get a minimal repro please? Thanks!
markm77 commentedon Sep 7, 2025
Sorry Youssef Fahmy (@Youssef1313) , will try to do this tomorrow!
markm77 commentedon Sep 8, 2025
Still working on this....
markm77 commentedon Sep 9, 2025
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:
and test with an
<Project Sdk="MSTest.Sdk/3.9.3">project test:I get test output:
And if I use
<Project Sdk="MSTest.Sdk/3.10.3">I get test outputwhere everything after
TestMethod: finishis removed.I have noted by a comment which line in
ClassInitialise()causes logging to occur afterTestMethod: finishbut 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.
Youssef1313 commentedon Sep 9, 2025
Thanks Mark Murphy (@markm77)
Can you please zip the whole project/solution?
Youssef1313 commentedon Sep 23, 2025
Investigated more. The
ConsoleLoggerProcessoris initiated the first time_webApplicationFactory.CreateClient()is called through this stack trace:CreateHostthen 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.
markm77 commentedon Sep 24, 2025
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
CreateHostis being called fromClassInitializerather thanTestMethodand MSTest will no longer accept console output from such an execution context?Is the solution to only call
CreateHostfromTestMethod?Youssef1313 commentedon Sep 24, 2025
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.
markm77 commentedon Sep 24, 2025
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.
Youssef1313 commentedon Sep 24, 2025
It's not that
CreateClientcreates execution context. It's more of "CreateClientis 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)
use custom sync console logger for MSTest
use custom sync console logger for MSTest
use custom sync console logger for MSTest
use custom sync console logger for MSTest
Update to MSTest 4 (#1721)