I am trying to use a text writer to write text to a file in .NET Core. The below code doesn't output anything to the file:
TextWriter writer = File.CreateText(@"...txt");
writer.Write("Hello World");
However, this does:
TextWriter writer = File.CreateText(@"...txt");
writer.Write("Hello World");
writer.Dispose();
Why is that? What does that extra line tell the program to do differently? I don't want to close the TextWriter because it is going to be writing logs, which are running constantly and indefinitely whilst my application is running.
How can I keep it open until the application stops running?
UPDATE
So the reason I want to do this, is I am using an SDK that writes its logs to a TextWriter:
TextWriterLogger(textWriter);
//Creates a logger that writes to a TextWriter. User is responsible for providing an instance of TextWriter that is thread safe.
But if I just enclose this in a using statement, logs won't be written because by the time they are ready to be written, the using statement will have executed and the TextWriter disposed.
Thanks