重定向也显示过程的输出流 [英] Redirect but also display process output stream
问题描述
我正在运行的内部版本,我希望能够因为它发生在查看进度。但我也想保存输出,如果构建有错误。
I am running a build, and I would like to be able to view the progress as it happens. But I would also like to save the output if the build has an error.
我知道我可以使用 Process.UseShellExecute = FALSE
和 RedirectStandardOutput
,但这只是故事的一部分。
I know I can use Process.UseShellExecute = false
, and RedirectStandardOutput
, but that's only part of the story.
我怎样才能做到这一点?
How can I do this?
推荐答案
更新
由于格雷格提及在下面的意见,MSBuild的可以写入到一个日志文件,同时输出到控制台开箱。
As Greg mentions in the comments below, MSBuild can write out to a log file while also outputting to console out of the box.
MSBuild [options] /filelogger /fileloggerparameters:LogFile=MSBuildLog.txt
请尝试以下简单的C#程序。这将需要重定向STDIN( Console.In
),并将其写入到一个或多个文件和标准输出( Console.Out
)
Try the following simple C# program. It will take the redirected STDIN (Console.In
) and write it to one or more files and STDOUT (Console.Out
).
using System;
using System.Collections.Generic;
using System.IO;
namespace RedirectToFile
{
class Program
{
static void Main(string[] args)
{
var buffer = new char[100];
var outputs = new List<TextWriter>();
foreach (var file in args)
outputs.Add(new StreamWriter(file));
outputs.Add(Console.Out);
int bytesRead;
do
{
bytesRead = Console.In.ReadBlock(buffer, 0, buffer.Length);
outputs.ForEach(o => o.Write(buffer, 0, bytesRead));
} while (bytesRead == buffer.Length);
outputs.ForEach(o => o.Close());
}
}
}
我用它来重定向从一个MSBuild批处理文件输出到磁盘的,而的还是输出到控制台窗口
I use it to redirect the output from an MSBuild batch file to disk whilst still outputting to the console window.
Usage: MSBuild [options] | RedirectToFile.exe MSBuildLog.txt
这篇关于重定向也显示过程的输出流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!