redirectsbutalsoshowsprocessoutputstream我正在运行构建,我希望能够看到发生了什么。但如果构建有错误,我也想保存输出。我知道我可以使用Process.UseShellExecute=false和RedirectStandardOutput,但这只是故事的一部分。我怎样才能做到这一点?更新正如Greg在下面的评论中提到的,MSBuild可以开箱即用地写入日志文件以及输出到控制台。MSBuild[options]/filelogger/fileloggerparameters:LogFile=MSBuildLog.txt尝试以下简单的C#程序。它将重定向STDIN(Console.In)并将其写入一个或多个文件和STDOUT(Console.Out)。使用系统;使用System.Collections.Generic;使用System.IO;namespaceRedirectToFile{classProgram{staticvoidMain(string[]args){varbuffer=newchar[100];变种输出=新列表();foreach(args中的var文件)outputs.Add(newStreamWriter(file));outputs.Add(Console.Out);intbytesRead;做{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批处理文件输出重定向到磁盘,同时仍然输出到控制台窗口。用法:MSBuild[选项]|RedirectToFile.exeMSBuildLog.txt也许是这样的?类Tee{privatereadonlystringm_programPath;私有只读字符串m_logPath;私人TextWriterm_writer;publicTee(stringprogramPath,stringlogPath){m_programPath=programPath;m_logPath=日志路径;)){varprocess=newProcess{StartInfo=newProcessStartInfo(m_programPath){RedirectStandardOutput=true,UseShellExecute=false}};process.OutputDataReceived+=OutputDataReceived;过程。开始();process.BeginOutputReadLine();对于process.Wait();}}privatevoidOutputDataReceived(objectsender,DataReceivedEventArgse){Console.WriteLine(e.Data);m_writer.WriteLine(e.Data);Thisanswershouldhelpyouefficientlyredirectthestandardin.NETOutputPS我不确定链接到SO上的另一个答案是答案还是评论以上是C#学习教程:重定向但也显示了过程输出流我分享的内容如果对大家有用,需要进一步了解C#,希望大家多多关注教程——本文整理自网络,不代表立场。如果涉及侵权请点击维权联系管理员删除如需转载请注明出处:
