Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.SetOut to StreamWriter, write to textfile constantly

I'm using the Console.SetOut method to write all my Console.Out.WriteLines to a file, and this works. The only problem is that it only writes everything to the textfile when I close my application instead of it writing whenever a Console.Out.WriteLine happens. Any ideas on how I can realise this?

How I do it: Before Application.Run();

FileStream writerOutput = new FileStream("Logging_Admin.txt", FileMode.Append, FileAccess.Write);
StreamWriter writer = new StreamWriter(writerOutput);
Console.SetOut(writer);

After Application.Run():

writer.Dispose();

Thanks.

like image 409
Fverswijver Avatar asked Feb 24 '10 15:02

Fverswijver


1 Answers

StreamWriter has an AutoFlush property. When set to true, you should get the result you need.

like image 180
Jens Avatar answered Sep 22 '22 07:09

Jens