Writing to File not saved until Closed [VC#]

Shaitan00

Well-known member
Joined
Aug 11, 2003
Messages
343
Location
Hell
I created a LOG file (ErrorLog) that I use to output/trace information from my application, problem is the I want to be able to read the file as things progress.
Given the way I coded it (see below) the information I write into the file is only saved afer I do the .CLOSE (meaning that if I open the .log file there is nothing written until I do the .close)
I would much rather be able to read the .log file while the application is running to see if anything is generated... (I dont want to have to CLOSE the file to see what was written to it).

Code:
StreamWriter ErrorLog = new StreamWriter(File.Open("App.log", FileMode.Append, FileAccess.Write, FileShare.ReadWrite));
string errMessage = DateTime.Now + ": " + sMessage;
ErrorLog.WriteLine(errMessage);
...
... do some work ...
...
ErrorLog.Close();

Any helps/hints/clues would be appreciated - I know there is a way I just cant recall how to get it to work.
Thanks,
 
Had trouble getting it to work and decided it would be best to just write one from scratch I could just package in a personal library or something...
 
Personally I would agree with Joe Mamma on this and would use the built in eventlog support - much better for administrators as your app will log to the same place as all other applications, plus there are standard tools for viewing, searching etc.
Failing that have you tried calling .Flush() and seeing if that makes a difference?
 
Back
Top