Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The main question is, do you have control over the source code of the program whose output you want logged and displayed?</p> <p>If so, then you have a couple options. Probably the easiest would be to "hijack" the Console's output stream with a compound TextWriter:</p> <pre><code>public class CompoundWriter:TextWriter { public readonly List&lt;TextWriter&gt; Writers = new List&lt;TextWriter&gt;(); public override void WriteLine(string line) { if(Writers.Any()) foreach(var writer in Writers) writer.WriteLine(line); } //override other TextWriter methods as necessary } ... //When the program starts, get the default Console output stream var consoleOut = Console.Out; //Then replace it with a Compound writer set up with a file writer and the normal Console out. var compoundWriter = new CompoundWriter(); compoundWriter.Writers.Add(consoleOut); compoundWriter.Writers.Add(new TextWriter("c:\temp\myLogFile.txt"); Console.SetOut(compoundWriter); //From now on, any calls to Console's Write methods will go to your CompoundWriter, //which will send them to the console and the file. </code></pre> <p>You can also use the Trace listeners to handle any output you want to go to both places:</p> <pre><code>Trace.Listeners.Clear(); Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); Trace.Listeners.Add(new TextWriterTraceListener(File.Open("C:\temp\myLogFile.txt"); //replace any call to Console.WriteLine() with Trace.WriteLine() </code></pre> <p>if you do NOT have control over the source code of the console app you want to "tee", and the console app does not require any input in the middle of its execution, then you can use a named pipe to get the output of the app and redirect it.</p> <pre><code>var appLocation = @"C:\temp\myApp.exe"; var pipeName = "ConsoleNamedPipe"; using(var namedPipe = new NamedPipeServerStream(pipeName, PipeDirection.In)) { var info = new ProcessStartInfo { FileName = "cmd.exe", Arguments = String.Format(@"/C {1} &gt;&gt;\\.\pipe\{0}", pipeName, appLocation), WindowStyle = ProcessWindowStyle.Hidden }; ConsoleProcess = Process.Start(info); pipe.WaitForConnection(); using (var reader = new StreamReader(pipe)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); Console.WriteLine(line); myFileWriter.WriteLine(line); } } } </code></pre> <p>This is a very simple example; you can provide more interaction by using a two-way pipe but it will require quite a bit more code on your end.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload