如何在将 stdout 和 stderr 输出到文件的同时实时查看控制台 [英] How Can I View the Console RealTime While Also Outputing stdout and stderr to a File

查看:54
本文介绍了如何在将 stdout 和 stderr 输出到文件的同时实时查看控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# 控制台应用程序,只需输入 c 即可在控制台上运行和观看:

I have a C# console application that I can run and watch at the console by just c typing:

programName.exe

我也可以通过以下方式将其写入文件:

I also can write this to a file by doing:

programName.exe > log.txt

但是,当我这样做时,我无法查看正在发生的输出.整个程序完成后,我只能查看日志文件.

But, when I do this, I can't view the output as it is happening. I'm force to just view the log file after the entire program has finished.

我怎样才能同时做到这两点?我想查看实时 stdout 和 stderr,但仍要写入该日志文件.有没有办法在不改变 c# 应用程序代码的情况下做到这一点?

How can I do both at the same time? I want to see real-time stdout and stderr, but still write to that log file. Is there a way to do this without altering the c# applications code?

推荐答案

如果可以选择使用 Windows PowerShell,那么您可以使用 Tee-Object 命令:

If using Windows PowerShell is an option, for you, you can use the Tee-Object command:

programName.exe | tee -file 'log.txt'

这会将标准输出同时重定向到控制台窗口和 log.txt.重定向 stdoutstderr 的解决方法是:

This will redirect stdout simultaneously to the console window and log.txt. A workaround to redirect both stdout and stderr is:

programName.exe 2>&1 | tee -file 'log.txt'

您还可以找到对 这个类似的问题很有帮助

You may also find the responses to this similar question helpful

这篇关于如何在将 stdout 和 stderr 输出到文件的同时实时查看控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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