reroute stderr/stdout from a monitored process

G

Guest

Hullo - I'm building a windows service in C# to monitor a process that generally runs as a console application.
What I need to do is reroute the output from the process in question so that it ends up in a logfile. Bit new to this, and have no idea where to start, suggestions?
 
C

Chad Myers

bixbym said:
Hullo - I'm building a windows service in C# to monitor a process that generally runs as a console application.
What I need to do is reroute the output from the process in question so that it ends up in a logfile. Bit new to this, and have no idea where to start, suggestions?

When you start a process (using the System.Diagnostics.Process class),
you can pass in a ProcessInfo object which has properties called
RedirectStandardOut and Error.

There are some caveats to using those though, so please read the .NET
Framework SDK docs on the Process class to understand them.

If you need more help, just post back.

-c
 
G

Guest

Never quite had to redirect another process's output in .NET yet but this code snippet will redirect all Debug/Trace message out put from current program to a text file

using System.Diagnostics
using System.IO
...

FileStream log = new FileStream("\\ServerLog.txt", FileMode.Create)
Debug.Listeners.Add(new TextWriterTraceListener(log))
Trace.Listeners.Add(new TextWriterTraceListener(log))
...

Trace.Writeline("Hello log file...")

Also check the documentation for the System.Diagnostics.Process class because I think you're going to have to redirect output from the ProcessInfo struct used to create the new process..

Hope it helps, anyway you have keywords to search on now..

--Richar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top