When I run msbuild at the command line it shows pretty colours in the console.
当我在命令行运行msbuild时,它在控制台中显示漂亮的颜色。
However when I run it from C# with Process.Start
, the output appears in black and white. How can I keep the colours?
但是,当我使用Process.Start从C#运行它时,输出显示为黑白。我该如何保持颜色?
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?
另外,虽然我们在这里,但是在BeginOutputReadLine之前运行Process.Start是否重要?输出会丢失吗?
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.
动机,对于那些感兴趣的人。我工作的项目使用自定义构建工具(重新发明*imho)。它使用msbuild但在复杂的间接层后面(上面的简化模型)。 Msbuild的有用颜色丢失了。我想保存它们。
3 个解决方案
#1
7
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.
Process.OutputDataReceived读取文本,而不是颜色。输出重定向功能位于此仅重定向标准输出文本下方,而不是控制台颜色属性。当您使用命令行中的> redirect运算符运行msbuild以将其输出发送到文本文件时,您会得到完全相同的结果。当您在记事本中打开文本文件时,您当然会看到平淡无奇的文本。
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 :)
解析重定向的输出以重新着色您自己的输出是非常不切实际的。你被困在了。然后,程序员不会经常抱怨IDE中错误列表窗口的外观:)
#2
4
Thats it, there is no other way to do it. Your code first starts the process and then appends the eventhandler. So there will be maybe some data that are lost, but that depends on how fast the cpu processes the code. You should better append the eventhandler first and then start the process. (see below)
多数民众赞成,没有其他办法可以做到这一点。您的代码首先启动该进程,然后附加事件处理程序。因此可能会丢失一些数据,但这取决于cpu处理代码的速度。您最好先添加eventhandler,然后再启动该过程。 (见下文)
using (var p = new Process())
{
p.StartInfo = new ProcessStartInfo("msbuild")
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
};
p.ErrorDataReceived += (s, e) => ErrorLine(e.Data);
p.OutputDataReceived += (s, e) => OutputLine(e.Data);
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.Start();
p.WaitForExit();
}
void ErrorLine(string text)
{
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.Error.WriteLine(text);
Console.ResetColor();
}
void OutputLine(string text)
{
Console.Error.WriteLine(text);
}
#3
1
I don't know about how can do this specifically for msbuild with all the warnings/errors/other things that do different colours, but you can change the console colour by using Console.ForegroundColor = ConsoleColor.Red;
before you write to it, and reset it with Console.ResetColor();
我不知道怎样才能为msbuild专门做所有警告/错误/其他不同颜色的事情,但你可以通过使用Console.ForegroundColor = ConsoleColor.Red来改变控制台颜色;在你写它之前,用Console.ResetColor()重置它;
So you would change ErrorDataRecieved subscription to do change the colour to red before you write, and reset the colour after you write the output.
因此,您可以更改ErrorDataRecieved订阅以在写入之前将颜色更改为红色,并在写入输出后重置颜色。
#1
7
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.
Process.OutputDataReceived读取文本,而不是颜色。输出重定向功能位于此仅重定向标准输出文本下方,而不是控制台颜色属性。当您使用命令行中的> redirect运算符运行msbuild以将其输出发送到文本文件时,您会得到完全相同的结果。当您在记事本中打开文本文件时,您当然会看到平淡无奇的文本。
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 :)
解析重定向的输出以重新着色您自己的输出是非常不切实际的。你被困在了。然后,程序员不会经常抱怨IDE中错误列表窗口的外观:)
#2
4
Thats it, there is no other way to do it. Your code first starts the process and then appends the eventhandler. So there will be maybe some data that are lost, but that depends on how fast the cpu processes the code. You should better append the eventhandler first and then start the process. (see below)
多数民众赞成,没有其他办法可以做到这一点。您的代码首先启动该进程,然后附加事件处理程序。因此可能会丢失一些数据,但这取决于cpu处理代码的速度。您最好先添加eventhandler,然后再启动该过程。 (见下文)
using (var p = new Process())
{
p.StartInfo = new ProcessStartInfo("msbuild")
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
};
p.ErrorDataReceived += (s, e) => ErrorLine(e.Data);
p.OutputDataReceived += (s, e) => OutputLine(e.Data);
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.Start();
p.WaitForExit();
}
void ErrorLine(string text)
{
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.Error.WriteLine(text);
Console.ResetColor();
}
void OutputLine(string text)
{
Console.Error.WriteLine(text);
}
#3
1
I don't know about how can do this specifically for msbuild with all the warnings/errors/other things that do different colours, but you can change the console colour by using Console.ForegroundColor = ConsoleColor.Red;
before you write to it, and reset it with Console.ResetColor();
我不知道怎样才能为msbuild专门做所有警告/错误/其他不同颜色的事情,但你可以通过使用Console.ForegroundColor = ConsoleColor.Red来改变控制台颜色;在你写它之前,用Console.ResetColor()重置它;
So you would change ErrorDataRecieved subscription to do change the colour to red before you write, and reset the colour after you write the output.
因此,您可以更改ErrorDataRecieved订阅以在写入之前将颜色更改为红色,并在写入输出后重置颜色。