Thursday 16 August 2012

Write exception to log file in C#

Add system.IO namespace
then write the following code
Create a text file named as "MyText.txt" in a location where the user having write access.( on which credential you are running the application.)

  FileStream fs = new FileStream("D:\\MyText.txt", FileMode.Append, FileAccess.Write);
                            StreamWriter sw = new StreamWriter(fs);
                            sw.WriteLine("Exception ="+"    "+ex.Message);
                            sw.Close();
                            fs.Close();

No comments:

Post a Comment