Click here to Skip to main content
15,891,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
FileStream fs1 = new FileStream("D:\\Error.txt", FileMode.OpenOrCreate,
 FileAccess.Write);
StreamWriter writer = new StreamWriter(fs1);
 // If file exists, text will be appended ; otherwise a new file will be created
writer.Write(string.Format("Message: {0}<br />{1}StackTrace :{2}{1}Date :{3}{1}-----------------------------------------------------------------------------{1}",
 ex.Message, Environment.NewLine, ex.StackTrace, DateTime.Now.ToString()));
  writer.Close();

//this code segment read data from the file.
 FileStream fs2 = new FileStream("D:\\Error.txt", FileMode.OpenOrCreate,
 FileAccess.Read);
 StreamReader reader = new StreamReader(fs2);
 reader.Close();

Here my code, save the exception with updated last exception..how to save multiple exceptions
C#

Posted
Updated 23-Jan-14 2:41am
v2

Take off the OpenOrCreate, which discards any previous file content.

By preference, replace the whole FileStream code with File.AppendAllText[^] - it opens teh file, appends the new text, and closes the file again in a single line. And it doesn't leave a FileStream object (fs1) sitting there undisposed.

Use File.ReadAllText as well instead of separate streams there as well.
 
Share this answer
 
try this

C#
string path ="D:\\Error.txt";
           File.AppendAllLines(path , new string[] { string.Format("Message: {0}<br />{1}StackTrace :{2}{1}Date :{3}{1}-----------------------------------------------------------------------------{1}",
          ex.Message, Environment.NewLine, ex.StackTrace, DateTime.Now.ToString()});
        string[] lines =  File.ReadAllLines(path);


use File.AppendAllLines[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900