mysteries of the disapearing text
-
I have written a small debug class so I can echo some statements to the ouput in VS and to a text file, using the StreamWriter class... The problem is, is that the text is never written to the file.
public class Debug { private StreamWriter _writer; private string path; public Debug(string path) { this.path = path; if( File.Exists(path) ) _writer = File.AppendText(path) else _writer = File.Create(path); } public void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); _writer.WriteLine(s); } }
Now this is fairly bog standard code foe writing to a file, and even stranger is if i replace the WriteLine procedure withpublic void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); StreamWriter sw = File.AppendText(this.path); sw.WriteLine(s); sw.Close(); }
The text is written to the file fine. Can any shed light on this.... What dumbass thing I am I doing wrong? -
I have written a small debug class so I can echo some statements to the ouput in VS and to a text file, using the StreamWriter class... The problem is, is that the text is never written to the file.
public class Debug { private StreamWriter _writer; private string path; public Debug(string path) { this.path = path; if( File.Exists(path) ) _writer = File.AppendText(path) else _writer = File.Create(path); } public void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); _writer.WriteLine(s); } }
Now this is fairly bog standard code foe writing to a file, and even stranger is if i replace the WriteLine procedure withpublic void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); StreamWriter sw = File.AppendText(this.path); sw.WriteLine(s); sw.Close(); }
The text is written to the file fine. Can any shed light on this.... What dumbass thing I am I doing wrong?Could it be because you are not explictly closing the stream in your original and maybe it is not being flushed? Personally, in my logging I do not try keep the file open all the time, I use:
public void Write(string message, LoggingLevel level)
{
// logging level and filename check code omittedStreamWriter logFile=null;
lock(this)
{
try
{
logFile = new StreamWriter(fileName, true);
logFile.WriteLine(message);
logFile.Close();
}
catch(Exception)
{
if(logFile!=null)
logFile.Close();
}
}if(isSystemDebugEnabled) // local class variable
System.Diagnostics.Debug.WriteLine(s);
}Rocky Moore <><
-
I have written a small debug class so I can echo some statements to the ouput in VS and to a text file, using the StreamWriter class... The problem is, is that the text is never written to the file.
public class Debug { private StreamWriter _writer; private string path; public Debug(string path) { this.path = path; if( File.Exists(path) ) _writer = File.AppendText(path) else _writer = File.Create(path); } public void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); _writer.WriteLine(s); } }
Now this is fairly bog standard code foe writing to a file, and even stranger is if i replace the WriteLine procedure withpublic void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); StreamWriter sw = File.AppendText(this.path); sw.WriteLine(s); sw.Close(); }
The text is written to the file fine. Can any shed light on this.... What dumbass thing I am I doing wrong?you have to fluch the stream. Another Post by NnamdiOnyeyiri l Website