[RESOLVED]Using textwriter
Hi all!
I code this function in order to create a log of my app.
My app calls this function every something wrong occurs. The file is created if it does not exist before.
The problem is that when I open the file, using the windows explorer, it is blank. So, any information was written in it. But, the function was called many times, as I could see debugging the app (the logMessage parameter is not empty anytime).
What is it wrong?
Code:
public void Log (String logMessage, String w) {
TextWriter textWriter = new StringWriter();
using (TextWriter streamWriter = new StreamWriter(w)) {
textWriter.Write("\r\nLog Entry : ");
textWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
textWriter.WriteLine(" :");
textWriter.WriteLine(" :{0}", logMessage);
textWriter.WriteLine("-------------------------------");
// Update the underlying file.
textWriter.Flush();
textWriter.Close();
}
}
Thank you in advance.
Re: [RESOLVED]Using textwriter
It si simple - you never write to streamWriter created on file, but you are writing to in memory textWriter, which content is never written to disk. Also, create the stream writer as arjay sugested with constructor accepting boolean flag indicating if the file shoul be just opened or created. You are creating always new file, so even if the log would be written to it, only the last will be kept.