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.