Introduction

This article demonstrates how to view errors in XML of any page and how to write an exception error and input meaningful information in XML files, and keep records by the day in a specified location.

Creating a Class

First create a class clsErrorHandler.cs
The output looks like Figure 1.
image1
The code snippet in Listing 1 creates a class.
  1. using System;
  2. using System.IO;
  3. using System.Web;
  4. using System.Xml;
  5. using System.Xml.Linq;
  6. using System.Text;
  7. using System.Data;
  8. using System.Configuration;
  9. /// <summary>Summary description for ErrHandlerClass</summary>
  10. public class clsErrorHandler
  11. {
  12. public clsErrorHandler()
  13. {
  14. // TODO: Add constructor logic here
  15. }
  16. #region "Write the Error details to the text file in the Error Log folder as per the year and month."
  17. /// <summary></summary>
  18. /// <param name="exc"></param>
  19. /// <remarks>Write the Error details to the text file in the Error Log folder as per the year and month.</remarks>
  20. public static void WriteError(Exception exc)
  21. {
  22. DataTable dt = new DataTable();
  23. DataSet ds = new DataSet();
  24. string struserid = string.Empty;
  25. string strUsername = string.Empty;
  26. string strIET = string.Empty;
  27. string strIEM = string.Empty;
  28. string strIES = string.Empty;
  29. string StrST = string.Empty;
  30. string pathname = D:\;
  31. if (System.IO.Directory.Exists((pathname + "ErrorLog\\" + System.DateTime.Now.Year.ToString() + "\\" + DateTime.Today.Month.ToString() + "\\")) == false)
  32. {
  33. System.IO.Directory.CreateDirectory((pathname + "ErrorLog\\" + System.DateTime.Now.Year.ToString() + "\\" + DateTime.Today.Month.ToString() + "\\"));
  34. }
  35. string ToaDate = DateTime.Today.Date.ToString("yyyyMMdd");
  36. string path = pathname + "ErrorLog\\" + System.DateTime.Now.Year.ToString() + "\\" + DateTime.Today.Month.ToString() + "\\";
  37. if (!File.Exists((path + "Log" + ToaDate + ".xml")))
  38. {
  39. File.Create((path + "Log" + ToaDate + ".xml")).Close();
  40. dt.Columns.Add("Date", System.Type.GetType("System.String"));
  41. dt.Columns.Add("Time", System.Type.GetType("System.String"));
  42. dt.Columns.Add("userid", System.Type.GetType("System.String"));
  43. dt.Columns.Add("userName", System.Type.GetType("System.String"));
  44. dt.Columns.Add("InnerExceptionType", System.Type.GetType("System.String"));
  45. dt.Columns.Add("InnerExceptionMessage", System.Type.GetType("System.String"));
  46. dt.Columns.Add("InnerExceptionSource", System.Type.GetType("System.String"));
  47. dt.Columns.Add("ErrorPage", System.Type.GetType("System.String"));
  48. dt.Columns.Add("ExceptionType", System.Type.GetType("System.String"));
  49. dt.Columns.Add("ExceptionMessage", System.Type.GetType("System.String"));
  50. dt.Columns.Add("TargetSite", System.Type.GetType("System.String"));
  51. dt.Columns.Add("StackTrace", System.Type.GetType("System.String"));
  52. dt.TableName = "ErrorLog";
  53. if (!string.IsNullOrEmpty(Convert.ToString(HttpContext.Current.Session["userid"])))
  54. {
  55. struserid = Convert.ToString(HttpContext.Current.Session["userid"]);
  56. strUsername = Convert.ToString(HttpContext.Current.Session["userName"]);
  57. }
  58. else
  59. {
  60. struserid = "None";
  61. strUsername = "None";
  62. }
  63. if (exc.InnerException != null)
  64. {
  65. strIET = exc.InnerException.GetType().ToString();
  66. strIEM = exc.InnerException.Message.ToString();
  67. strIES = exc.InnerException.Source.ToString();
  68. }
  69. else
  70. {
  71. strIET = "None";
  72. strIEM = "None";
  73. strIES = "None";
  74. }
  75. if (exc.StackTrace != null)
  76. {
  77. StrST = exc.Source.ToString();
  78. }
  79. else
  80. {
  81. StrST = "None";
  82. }
  83. dt.Rows.Add(DateTime.Now.ToString("dd-MMM-yyyy"), DateTime.Now.ToLongTimeString(), struserid, strUsername, strIET, strIEM, strIES, HttpContext.Current.Request.Url.ToString(), exc.GetType().ToString(), exc.Message.ToString(), exc.TargetSite.ToString(), StrST);
  84. dt.WriteXml((path + "Log" + ToaDate + ".xml"));
  85. dt = null;
  86. }
  87. else
  88. {
  89. ds.ReadXml((path + "Log" + ToaDate + ".xml"));
  90. if (ds.Tables.Contains("ErrorLog"))
  91. {
  92. dt = ds.Tables["ErrorLog"];
  93. if (dt.Rows.Count > 0)
  94. {
  95. if (!string.IsNullOrEmpty(Convert.ToString(HttpContext.Current.Session["userName"])))
  96. {
  97. struserid = Convert.ToString(HttpContext.Current.Session["userName"]);
  98. strUsername = Convert.ToString(HttpContext.Current.Session["userName"]);
  99. }
  100. else
  101. {
  102. struserid = "None";
  103. strUsername = "None";
  104. }
  105. if (exc.InnerException != null)
  106. {
  107. strIET = exc.InnerException.GetType().ToString();
  108. strIEM = exc.InnerException.Message.ToString();
  109. strIES = exc.InnerException.Source.ToString();
  110. }
  111. else
  112. {
  113. strIET = "None";
  114. strIEM = "None";
  115. strIES = "None";
  116. }
  117. if (exc.StackTrace != null)
  118. {
  119. StrST = exc.Source.ToString();
  120. }
  121. else
  122. {
  123. StrST = "None";
  124. }
  125. dt.Rows.Add(DateTime.Now.ToString("dd-MMM-yyyy"), DateTime.Now.ToLongTimeString(), struserid, strUsername, strIET, strIEM, strIES, HttpContext.Current.Request.Url.ToString(), exc.GetType().ToString(), exc.Message.ToString(), exc.TargetSite.ToString(), StrST);
  126. dt.WriteXml((path + "Log" + ToaDate + ".xml"));
  127. }
  128. }
  129. dt = null;
  130. ds = null;
  131. }
  132. }
  133. #endregion
  134. }
Listing 1
N.B
  1. Here we used userid and Username for getting more precise data, it is optional.
  2. You can use also use web.config to set addpath location

Positioning clsErrorHandler in catch

Now put the code in catch exception block as shown in Listing 2
The code snippet in Listing 2 shows the catch clsErrorHandler used on the page.
  1. try
  2. {
  3. //Create a Exception.
  4. int y = 0;
  5. int Z = 3 / y;
  6. }
  7. catch (Exception ex)
  8. {
  9. clsErrorHandler.WriteError(ex);
  10. }
Listing 2
Output in XML Format
image2
Figure 2
Now you can open a location of the file which contains the name Log(Date).xml. Open in notepad or browser.

Summary

In this article, I discussed how we can create an Error log that contains Error details when the error comes in exception. It writes data in the file and saves it in a specified location. Here you can save by year , month, and/or day. It helps you to see the precise error details. Here it writes in XML file and another error comes then it's appended in the same file.