Here is the code:

StreamWriter stream = new StreamWriter(“output.txt” );
stream.AutoFlush = true;
Console.SetOut(stream);

Here we are 1st creating a StreamWriter which writes the output to a text file named “output.txt”. Then, we are setting it’s AutoFlush property to tru so that the Stream does not buffer anything and writes the data written to it directly to the file. And then, we are setting the Stream as the output stream for the Console. After which, all output to the Console will be written to the output.txt instead of getting printed on screen.

It can also be useful as a poor man’s logging method as this does not require any external libraries and can be implemented quickly for some simple projects.

Hope it helps someone,

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList