如何从MSBuild的输出保持颜色的? [英] How to keeps colours from msbuild output?

查看:119
本文介绍了如何从MSBuild的输出保持颜色的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在命令行中运行的MSBuild它显示在控制台pretty的颜色。

然而,当我运行它从C#与的Process.Start ,输出会出现在黑色和白色。我如何可以保持颜色?

  VAR信息=新的ProcessStartInfo(MSBuild的)
{
    UseShellExecute =假,
    CreateNoWindow = TRUE,
    RedirectStandardError = TRUE,
    RedirectStandardOutput = TRUE,
};

使用(VAR P =的Process.Start(信息))
{
    p.ErrorDataReceived + =(S,E)=> Console.Error.WriteLine(e.Data);
    p.OutputDataReceived + =(S,E)=> Console.WriteLine(e.Data);
    p.BeginErrorReadLine();
    p.BeginOutputReadLine();
    p.WaitForExit();
}
 

另外,虽然我们在这里,它的问题比我跑的Process.Start BeginOutputReadLine ?将任何输出会丢失?


动机,对于那些有兴趣。我工作在一个项目中使用自定义生成工具(重新发明轮子恕我直言)。它使用的MSBuild但其背后间接迂回层(上面的简化模型)。 MSBUILD乐于助人的颜色都将丢失。我想救他们。

解决方案

  p.OutputDataReceived + =(S,E)=> Console.WriteLine(e.Data);
 

Process.OutputDataReceived读取文本,而不是颜色。输出重定向功能,就是下面这只是重定向标准输出文本,而不是控制台颜色属性。你得到同样的事情,当你运行的MSBuild与> 命令行重定向操作符的输出发送到一个文本文件中。当然,你会看到乏味的文字,当你在记事本中打开文本文件。

解析重定向输出到重彩自己的产量大幅度不切实际的。你被卡住平淡。再说,程序员不经常抱怨在IDE :)

错误列表窗口的外观和感觉

When I run msbuild at the command line it shows pretty colours in the console.

However when I run it from C# with Process.Start, the output appears in black and white. How can I keep the colours?

var info = new ProcessStartInfo("msbuild")
{
    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardError = true,
    RedirectStandardOutput = true,            
};

using (var p = Process.Start(info) )
{
    p.ErrorDataReceived += (s, e) => Console.Error.WriteLine(e.Data);
    p.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
    p.BeginErrorReadLine();
    p.BeginOutputReadLine();
    p.WaitForExit();
}

Also, while we're here, does it matter than I run Process.Start before BeginOutputReadLine ? Will any output be lost?


Motivation, for those interested. A project I work on uses a custom build tool (re-inventing the wheel imho). It uses msbuild but behind convoluted layers of indirection (simplified model above). Msbuild's helpful colours are lost. I'd like to save them.

解决方案

   p.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);

Process.OutputDataReceived reads text, not colors. The output redirection feature that's underneath this only redirect stdout text, not the console color attributes. You get the exact same thing when you run msbuild with the > redirect operator from the command line to send its output to a text file. You'll of course see bland text when you open the text file in Notepad.

Parsing the redirected output to re-color your own output is drastically impractical. You are stuck with bland. Then again, programmers don't complain often about the look-and-feel of the Error List window in the IDE :)

这篇关于如何从MSBuild的输出保持颜色的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆