如何获取由进程启动的Msbuild错误?

时间:2019-01-15 12:33:24

标签: c# wpf process msbuild

我正在创建一个小程序,该程序将启动msbuild来编译应用程序。我使用进程,但无法获取所生成的错误。如果我从cmd消息中以红色显示的状态启动msbuild,则我理解ErrorDataReceived事件应该检测到这些错误,但是它不起作用

    process = new Process();
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.OutputDataReceived += new DataReceivedEventHandler(OutputHandlerData);
    process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandlerError);
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.FileName = "test.bat";
    process.Start();
    process.BeginOutputReadLine();
    process.BeginErrorReadLine();

    private void OutputHandlerError(object sendingProcess, DataReceivedEventArgs outLine)
    {
        log = log + outLine.Data + "\n";
        WriteInLog(log);
    }

        private void OutputHandlerData(object sendingProcess, DataReceivedEventArgs outLine)
    {
        if (outLine.Data != null)
        {
            if (outLine.Data.Contains("can not find"))
            {
                log = log + outLine.Data;
                WriteInLog(log);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

最后,我更改了MSBuild命令的详细说明,现在我可以输出带有OutputDataReceived事件的所有句子,而不会阻止应用程序,因为它显示的信息少得多。我还保存了一个文件,其中包含有关编译的更多信息。

MSBuild.exe -v:q -clp:ErrorsOnly;NoSummary -fl -flp:logfile=MyProjectOutput.log;verbosity=normal ..\MySolution.sln

-v:q-> verbose = quiet
-clp:ErrorsOnly; NoSummary->仅显示错误
-fl -flp:logfile = MyProjectOutput.log; verbosity = normal->创建一个名为MyProjectOutput.log的文件以保存verbosity = normal的日志(显示错误,警告,高重要性消息,正常重要性消息)