9

It appears msbuild writes all output (including error output) to standard output.

Is there some way to have it write error output (what's normally output in red) to standard error instead?

I'm writing a .NET application with a WPF and console interface and calling msbuild using System.Diagnostics.Process. I'd like to be able to distinguish error output to the user somehow.

Is there a better of separating the output than looking for "error " in each line or using Microsoft.Build directly + a custom logger?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Matt
  • 865
  • 2
  • 10
  • 16
  • I was supprised to see that this was true. Many build pipelines available today rely on standard error to halt the build. Wierd stuff. – Martin Aug 14 '18 at 13:58

2 Answers2

11

Take a look at the MSBUILD.EXE command line arguments page. Specifically the consoleloggerparameters switch.

You can use /clp:ErrorsOnly to display only errors in the console output.

If you need the rest of the output, include an errors only file log with

/fl4 /flp4:errorsOnly;logfile=MSBuild.Errors.log

then monitor the file for new lines.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Nicodemeus
  • 4,005
  • 20
  • 23
1

I know you said you wanted to avoid a custom logger, but... I also wanted error output on stderr and found that writing a custom logger was not that painful - 1 class with 1 method with 1 statement:

using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;

public class ErrorOnlyLogger : Logger
{
    public override void Initialize(IEventSource eventSource)
    {
        eventSource.ErrorRaised += (s, e) => {
            System.Console.Error.WriteLine(
                "{0}({1},{2}): error {3}: {4} [{5}]",
                e.File, e.LineNumber, e.ColumnNumber, e.Code, e.Message, e.ProjectFile);
        };
    }
}

This uses the same error formatting as msbuild. This works with command-line msbuild 14.0.25420.1. The code references Microsoft.Build.Utilities.Core.dll and Microsoft.Build.Framework.dll in C:\Program Files (x86)\MSBuild\14.0\Bin. I add /logger:ErrorOnlyLogger,path\ErrLogger.dll to the command line to invoke it.

unbob
  • 331
  • 3
  • 7
  • interesting suggestion. I am trying to solve a similar problem to the question author, and it looks promising. Additional info for subsequent readers: https://msdn.microsoft.com/en-us/library/ms171471.aspx – Roger Hill Apr 26 '17 at 21:47