Search code examples
c#cmdstdoutstderrwindows-console

How Can I View the Console RealTime While Also Outputing stdout and stderr to a File


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.

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?


Solution

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

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

    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