Is it possible to prevent MSBuild printing colouri

2019-08-02 08:13发布

问题:

I've got my windows shell configured as displaying black text on a white background. This makes it almost impossible to read the default msbuild output due to the very pale colours (especially the yellow warnings).

Therefore I'd like to try one of the following, but I can't work out if it is possible.

  1. I'd like to set a global setting to permanently turn off colourized output in msbuild; or
  2. If (1) isn't possible is it possible to turn this output per-invocation (e.g. with command line arguments).

Does anyone know how to do one of the above?

回答1:

In MSBuild 4.0 this is possible using the /consoleloggerparameters or /clp switch:

msbuild C:\some_path\your.sln /clp:disableconsolecolor

Alternatively, for previous MSBuild engines, this is possible using PowerShell:

Out-Host will display the default color:

Powershell -Command "msbuild C:\some_path\your.sln | out-host"

Write-Host will let you customize the colors:

Powershell -Command "msbuild C:\some_path\your.sln | write-host -foreground "white""


回答2:

To completely disable colors use the /clp:disableconsolecolor option when invoking MSBuild.exe (for more information on the /clp option run MSBuild.exe /?).

Update as @KMoraz has commented, and updated his answer to, this only works with MSBuild 4.0 onwards.

If you want to disable color output you can also use the following (which will not work with MSBuild 4.0):

MSBuild.exe arguments > CON 2>&1

This got me curious ;-) so here is one more option that should work with all versions of MSBuild.exe and doesn't rely on CON redirection:

MSBuild.exe arguments 2>&1| findstr /r ".*"

Basically, what happens is that all lines of output are piped through findstr.exe since that uses a pattern to match "everything", all lines are simply output again, but loosing their attributes (color) information. In my tests the 2>&1 (redirect stderr to stdout) was not really necessary, as it looks MSBuild doesn't output any (colored) messages to stderr, but I added it for good measure.