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.
The code snippet in Listing 1 creates a class.
- using System;
- using System.IO;
- using System.Web;
- using System.Xml;
- using System.Xml.Linq;
- using System.Text;
- using System.Data;
- using System.Configuration;
-
-
- public class clsErrorHandler
- {
- public clsErrorHandler()
- {
-
- }
-
- #region "Write the Error details to the text file in the Error Log folder as per the year and month."
-
-
-
- public static void WriteError(Exception exc)
- {
- DataTable dt = new DataTable();
- DataSet ds = new DataSet();
- string struserid = string.Empty;
- string strUsername = string.Empty;
- string strIET = string.Empty;
- string strIEM = string.Empty;
- string strIES = string.Empty;
- string StrST = string.Empty;
-
- string pathname = D:\;
- if (System.IO.Directory.Exists((pathname + "ErrorLog\\" + System.DateTime.Now.Year.ToString() + "\\" + DateTime.Today.Month.ToString() + "\\")) == false)
- {
- System.IO.Directory.CreateDirectory((pathname + "ErrorLog\\" + System.DateTime.Now.Year.ToString() + "\\" + DateTime.Today.Month.ToString() + "\\"));
- }
- string ToaDate = DateTime.Today.Date.ToString("yyyyMMdd");
- string path = pathname + "ErrorLog\\" + System.DateTime.Now.Year.ToString() + "\\" + DateTime.Today.Month.ToString() + "\\";
- if (!File.Exists((path + "Log" + ToaDate + ".xml")))
- {
- File.Create((path + "Log" + ToaDate + ".xml")).Close();
-
-
- dt.Columns.Add("Date", System.Type.GetType("System.String"));
- dt.Columns.Add("Time", System.Type.GetType("System.String"));
- dt.Columns.Add("userid", System.Type.GetType("System.String"));
- dt.Columns.Add("userName", System.Type.GetType("System.String"));
- dt.Columns.Add("InnerExceptionType", System.Type.GetType("System.String"));
- dt.Columns.Add("InnerExceptionMessage", System.Type.GetType("System.String"));
- dt.Columns.Add("InnerExceptionSource", System.Type.GetType("System.String"));
- dt.Columns.Add("ErrorPage", System.Type.GetType("System.String"));
- dt.Columns.Add("ExceptionType", System.Type.GetType("System.String"));
- dt.Columns.Add("ExceptionMessage", System.Type.GetType("System.String"));
- dt.Columns.Add("TargetSite", System.Type.GetType("System.String"));
- dt.Columns.Add("StackTrace", System.Type.GetType("System.String"));
-
- dt.TableName = "ErrorLog";
-
- if (!string.IsNullOrEmpty(Convert.ToString(HttpContext.Current.Session["userid"])))
- {
- struserid = Convert.ToString(HttpContext.Current.Session["userid"]);
- strUsername = Convert.ToString(HttpContext.Current.Session["userName"]);
- }
- else
- {
- struserid = "None";
- strUsername = "None";
- }
- if (exc.InnerException != null)
- {
-
- strIET = exc.InnerException.GetType().ToString();
- strIEM = exc.InnerException.Message.ToString();
- strIES = exc.InnerException.Source.ToString();
- }
- else
- {
- strIET = "None";
- strIEM = "None";
- strIES = "None";
- }
- if (exc.StackTrace != null)
- {
- StrST = exc.Source.ToString();
- }
- else
- {
- StrST = "None";
- }
-
- 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);
- dt.WriteXml((path + "Log" + ToaDate + ".xml"));
- dt = null;
- }
- else
- {
- ds.ReadXml((path + "Log" + ToaDate + ".xml"));
- if (ds.Tables.Contains("ErrorLog"))
- {
- dt = ds.Tables["ErrorLog"];
- if (dt.Rows.Count > 0)
- {
- if (!string.IsNullOrEmpty(Convert.ToString(HttpContext.Current.Session["userName"])))
- {
- struserid = Convert.ToString(HttpContext.Current.Session["userName"]);
- strUsername = Convert.ToString(HttpContext.Current.Session["userName"]);
- }
- else
- {
- struserid = "None";
- strUsername = "None";
- }
- if (exc.InnerException != null)
- {
-
- strIET = exc.InnerException.GetType().ToString();
- strIEM = exc.InnerException.Message.ToString();
- strIES = exc.InnerException.Source.ToString();
- }
- else
- {
- strIET = "None";
- strIEM = "None";
- strIES = "None";
- }
- if (exc.StackTrace != null)
- {
- StrST = exc.Source.ToString();
- }
- else
- {
- StrST = "None";
- }
-
- 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);
- dt.WriteXml((path + "Log" + ToaDate + ".xml"));
- }
- }
- dt = null;
- ds = null;
- }
- }
- #endregion
- }
Listing 1
N.B
- Here we used userid and Username for getting more precise data, it is optional.
- 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.
- try
- {
-
- int y = 0;
- int Z = 3 / y;
- }
- catch (Exception ex)
- {
- clsErrorHandler.WriteError(ex);
- }
Listing 2
Output in XML Format
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.