I am testing a custom msbuild task. So I have a test function like this, following this answer: https://stackoverflow.com/a/10718438/4126652
The basic idea is I build a test project that tries to run my task. I want the build logs that include my task logs to appear in the test console output.
[Fact]
public void TestFullProjectBuild()
{
List<ILogger> loggers = new() {new ConsoleLogger()}; // Ilogger, ConsoleLogger is from using Microsoft.Build.Framework, using Microsoft.Build.Logging
var projectCollection = new ProjectCollection();
projectCollection.RegisterLoggers(loggers);
string fixtureDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "fixtures");
string projPath = Path.Combine(fixtureDir, "TestProject.csproj.xml");
var project = projectCollection.LoadProject(projPath);
var ok = project.Build();
projectCollection.UnregisterAllLoggers();
Assert.True(ok);
}
Now this doesn't produce any build logs to my test console output. Searching in google and stackoverflow tells me that the only way to get console output in Xunit is to use ITestOutputHelper
.
But I don't know how to pass that as the logger to MSBuild.
Any help is appreciated. I want my project.Build()
console output to be captured by the test output