vb.netstdoutstreamwriterredirectstandardoutput

How to write to a StreamWriter without flush or close


I am trying to redirect the standard output to a file for logging purpose.

Dim tmp As TextWriter = Console.Out  
Dim sw As StreamWriter = File.AppendText("log.txt"))
Console.SetOut(sw)
Console.Write("Test")

but it looks like nothing get written until I call Flush() or Close() on the StreamWriter. Since the stream is going to stay open for a long time (Until the application closes), I would like the buffer to write directly to the file, otherwise it might become really big. How can I do that ?


Solution

  • You can set your StreamWriter.AutoFlush property to true:

    AutoFlush to true means that data will be flushed from the buffer to the stream after each write operation, but the encoder state will not be flushed