1

I'm trying to capture output of external program (its a console application) that I started from .net application. This is how is started.

var process = new Process();
process.StartInfo = new ProcessStartInfo
{
    FileName = "c:\app.exe,
    Arguments = "-x 0 -server http://foo.com",
    RedirectStandardOutput = true,
    RedirectStandardInput = true,
    RedirectStandardError = true,
    UseShellExecute = false
};
process.Start();
process.OutputDataReceived += Process_OutputDataReceived;
process.ErrorDataReceived += Process_ErrorDataReceived;
process.BeginOutputReadLine();
process.WaitForExit();

This console application is running until its manually stopped and is constantly writing something to the console window. What I would like to do is to capture output from the console and if there was a line with, lets say "ERROR", I would restart the process.

The code above does not work. Process_OutputDataReceived is called only when I close the app.exe console window. I guess I could write the output to some file and analyze that file, but i dont like that too much, since i would need to open/read that file every second.

Is there a way to capture the output of app.exe as its being written to the console?

edit:

writing output to the file is option, but file can get very, very big until i get a line with "ERROR" and only at that point i will restart the process and delete the file. Open/read few 100MB+ file every second until then is not the way i would like to do this.

bigsako
  • 43
  • 5
  • Possible duplicate of [Capturing process output via OutputDataReceived event](https://stackoverflow.com/questions/11631443/capturing-process-output-via-outputdatareceived-event) – NineBerry Jul 05 '18 at 20:01
  • why dont your write output on texxtfile? – TAHA SULTAN TEMURI Jul 05 '18 at 20:08
  • I guess thats the option, but only if there is no other way to read the output. Plus, this file can get very big, very fast. Deleting file would require process to be stopped, which i dont want to do, until i get "ERROR" in output. But as i said, the file can get very big until i get the error. – bigsako Jul 05 '18 at 20:25
  • Writing to stdout is a perfectly valid way of handling output. Are you sure the application you're running is writing to stdout as it goes, instead of all at once at the end, and that it is doing so by writing to the standard output stream instead of manipulating the Console window? –  Jul 05 '18 at 20:35
  • The console application im running is some external app, and yes, its writing to console window line by line, not at the end since it can run days/weeks and is showing output as it happens. example would be, lets say ping.exe with 1 million repetitions – bigsako Jul 05 '18 at 20:40
  • @bigsako Okay. Try also calling `BeginErrorReadLine()`. Are you sure the target application is writing to stdout instead of stderr? Are you sure the target application is flushing the stream after each write? –  Jul 05 '18 at 20:48
  • If it's none of those things, I have no idea. I see nothing 'wrong' with the code presented in the question (besides the notable absence of `BeginErrorReadLine()`). I assume the target application isn't writing to stdout, or isn't flushing the stream after each write, or it is doing something else unexpected. –  Jul 05 '18 at 20:58
  • Not working with BeginErrorReadLine(). I dont know how its writing to the output since i dont have source of the console app. I only run it. – bigsako Jul 05 '18 at 21:06
  • @bigsako To ensure our assumptions about the target application are correct, try doing a console redirect from the command prompt, e.g. `app.exe > outfile.txt`. Let it run a minute, then kill it and examine the file. You can also redirect stderr from the command prompt by doing `app.exe > outfile.txt 2>&1`. If that works, then we know its an issue with your code. If it doesn't, the target application isn't writing to stdout or stderr as expected. Beyond that, I'm afraid I have no other ideas :( –  Jul 05 '18 at 21:09

1 Answers1

0

In your Process_OutputDataReceived method implementation, look for "ERROR" in data like below:

 private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data == "ERROR")
            {
                //Restart your App
            }
        }
marak
  • 260
  • 3
  • 19
  • 1
    Like i said: Process_OutputDataReceived is called only when I close the app.exe console window. – bigsako Jul 05 '18 at 20:34
  • That may be because the app.exe is not writing onto the console until its closed. Is it writing anything to the console before you attempt to close it? May be you need to post your app.exe code to understand what its doing – marak Jul 05 '18 at 20:52
  • It is writing to the output as it happens. And its 3rd party app, dont have source code – bigsako Jul 05 '18 at 20:55
  • did you see if the data is somehow going to "Process_ErrorDataReceived" method? – marak Jul 05 '18 at 21:13