msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

MSBuild 18 breaks custom loggers that inherit from `ConsoleLogger`

Open rainersigwald opened this issue 2 months ago • 2 comments

A Microsoft-internal team adopting MSBuild 18/Visual Studio 2026 reported that their custom logger was crashing with the callstack

1>Microsoft.Build.Exceptions.InternalLoggerException: The build stopped unexpectedly because of an unexpected logger failure. ---> System.NullReferenceException: Object reference not set to an instance of an object.
1>   at Microsoft.Build.BackEnd.Logging.ParallelConsoleLogger.WriteMessageAligned(String message, Boolean prefixAlreadyWritten, Int32 prefixAdjustment)
1>   at Microsoft.Build.BackEnd.Logging.ParallelConsoleLogger.DisplayDeferredProjectStartedEvent(BuildEventContext e)
1>   at Microsoft.Build.BackEnd.Logging.ParallelConsoleLogger.DisplayDeferredStartedEvents(BuildEventContext e)
1>   at Microsoft.Build.BackEnd.Logging.ParallelConsoleLogger.MessageHandler(Object sender, BuildMessageEventArgs e)
1>   at Microsoft.Build.Framework.BuildMessageEventHandler.Invoke(Object sender, BuildMessageEventArgs e)
1>   at Microsoft.Build.BackEnd.Logging.EventSourceSink.Consume(BuildEventArgs buildEvent)
1>   --- End of inner exception stack trace ---
1>   at Microsoft.Build.Exceptions.InternalLoggerException.Throw(Exception innerException, BuildEventArgs e, String messageResourceName, Boolean initializationException, String[] messageArgs)
1>   at Microsoft.Build.BackEnd.Logging.EventSourceSink.Consume(BuildEventArgs buildEvent)
1>   at Microsoft.Build.BackEnd.Logging.EventSourceSink.RaiseAnyEvent(BuildEventArgs buildEvent)

I helped debug and the problem arose when https://github.com/dotnet/msbuild/pull/12336 changed the no-configuration default backing logger for ConsoleLogger._consoleLogger from SerialConsoleLogger to ParallelConsoleLogger.

The custom logger in question overrides ConsoleLogger.Initialize and does not call base.Initialize. That and BuildFinishedHandler are the only entrypoints to CheckIfOutputSupportsAlignment() so it didn't get called, meaning it didn't get to run

https://github.com/dotnet/msbuild/blob/a73379bc4af3599eb79b899a7c4c56a6c0dff9ff/src/Build/Logging/ParallelLogger/ParallelConsoleLogger.cs#L104

so _consoleOutputAligner is null in

https://github.com/dotnet/msbuild/blob/a73379bc4af3599eb79b899a7c4c56a6c0dff9ff/src/Build/Logging/ParallelLogger/ParallelConsoleLogger.cs#L1401

rainersigwald avatar Nov 10 '25 20:11 rainersigwald

@nohwnd I'm not sure if this SerialConsoleLogger -> ParallelConsoleLogger is related to the race you are investigating with VSTest terminal logger, but thought it's worth noting this to you :)

Youssef1313 avatar Nov 16 '25 20:11 Youssef1313

I don't think it is related, but thanks!

nohwnd avatar Nov 18 '25 10:11 nohwnd