Pages

Tuesday, September 3, 2013

Create Error Log or Exception Log - C#


Red code Creates a text file "ErrorLog.txt"and write exception message, when any exception occurs for the very first time and then it appends the exceptions to the text file when further exceptions occurs.

        try
        {
          // code here...............
        }
        catch (Exception ex)
        {   
         File.AppendAllText(MapPath("ErrorLog.txt"), ex.Message.ToString() + "|" + DateTime.Now + Environment.NewLine);     
        }



Output:(ErrorLog.txt)

exception1|currentDatetime
exception2|currentDatetime 
....
...

Real Output:

Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment. |9/2/2013 6:46:40 PM
Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment. |9/2/2013 6:46:42 PM
Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment. |9/2/2013 6:55:57 PM


No comments:

Post a Comment