Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mirroring console output to a file

In a C# console application, is there a smart way to have console output mirrored to a text file?

Currently I am just passing the same string to both Console.WriteLine and InstanceOfStreamWriter.WriteLine in a log method.

like image 700
xyz Avatar asked Jan 07 '09 14:01

xyz


People also ask

How do I save a console output to a text file?

the shortcut is Ctrl + Shift + S ; it allows the output to be saved as a text file, or as HTML including colors!

How do I display console output?

Press CTRL + F5 to see your output. This will wait the console screen until you press any key.


2 Answers

This may be some kind of more work, but I would go the other way round.

Instantiate a TraceListener for the console and one for the log file; thereafter use Trace.Write statements in your code instead of Console.Write. It becomes easier afterwards to remove the log, or the console output, or to attach another logging mechanism.

static void Main(string[] args) {     Trace.Listeners.Clear();      TextWriterTraceListener twtl = new TextWriterTraceListener(Path.Combine(Path.GetTempPath(), AppDomain.CurrentDomain.FriendlyName));     twtl.Name = "TextLogger";     twtl.TraceOutputOptions = TraceOptions.ThreadId | TraceOptions.DateTime;      ConsoleTraceListener ctl = new ConsoleTraceListener(false);     ctl.TraceOutputOptions = TraceOptions.DateTime;      Trace.Listeners.Add(twtl);     Trace.Listeners.Add(ctl);     Trace.AutoFlush = true;      Trace.WriteLine("The first line to be in the logfile and on the console."); } 

As far as I can recall, you can define the listeners in the application configuration making it possible to activate or deactivate the logging without touching the build.

like image 107
Oliver Friedrich Avatar answered Sep 18 '22 19:09

Oliver Friedrich


This is a simple class which subclasses TextWriter to allow redirection of the input to both a file and the console.

Use it like this

  using (var cc = new ConsoleCopy("mylogfile.txt"))   {     Console.WriteLine("testing 1-2-3");     Console.WriteLine("testing 4-5-6");     Console.ReadKey();   } 

Here is the class:

class ConsoleCopy : IDisposable {    FileStream fileStream;   StreamWriter fileWriter;   TextWriter doubleWriter;   TextWriter oldOut;    class DoubleWriter : TextWriter   {      TextWriter one;     TextWriter two;      public DoubleWriter(TextWriter one, TextWriter two)     {       this.one = one;       this.two = two;     }      public override Encoding Encoding     {       get { return one.Encoding; }     }      public override void Flush()     {       one.Flush();       two.Flush();     }      public override void Write(char value)     {       one.Write(value);       two.Write(value);     }    }    public ConsoleCopy(string path)   {     oldOut = Console.Out;      try     {       fileStream = File.Create(path);        fileWriter = new StreamWriter(fileStream);       fileWriter.AutoFlush = true;        doubleWriter = new DoubleWriter(fileWriter, oldOut);     }     catch (Exception e)     {       Console.WriteLine("Cannot open file for writing");       Console.WriteLine(e.Message);       return;     }     Console.SetOut(doubleWriter);   }    public void Dispose()   {     Console.SetOut(oldOut);     if (fileWriter != null)     {       fileWriter.Flush();       fileWriter.Close();       fileWriter = null;     }     if (fileStream != null)     {       fileStream.Close();       fileStream = null;     }   }  } 
like image 37
Christian Avatar answered Sep 18 '22 19:09

Christian