Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make msbuild write error output to stderr?

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?

like image 295
Matt Avatar asked Aug 14 '13 15:08

Matt


2 Answers

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.

like image 80
Nicodemeus Avatar answered Sep 26 '22 06:09

Nicodemeus


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.

like image 34
unbob Avatar answered Sep 24 '22 06:09

unbob