How to CreateError Log Report In ASP.NET By The Day

In this article, we will learn about Error Log Report by the day in ASP.NET.

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.     
  10. /// <summary>Summary description for ErrHandlerClass</summary>    
  11. public class clsErrorHandler    
  12. {    
  13.     public clsErrorHandler()    
  14.     {    
  15.         // TODO: Add constructor logic here    
  16.     }    
  17.   
  18.     #region "Write the Error details to the text file in the Error Log folder as per the year and month."    
  19.     /// <summary></summary>    
  20.     /// <param name="exc"></param>    
  21.     /// <remarks>Write the Error details to the text file in the Error Log folder as per the year and month.</remarks>    
  22.     public static void WriteError(Exception exc)    
  23.     {    
  24.         DataTable dt = new DataTable();    
  25.         DataSet ds = new DataSet();    
  26.         string struserid = string.Empty;    
  27.         string strUsername = string.Empty;    
  28.         string strIET = string.Empty;    
  29.         string strIEM = string.Empty;    
  30.         string strIES = string.Empty;    
  31.         string StrST = string.Empty;    
  32.     
  33.         string pathname = D:\;    
  34.         if (System.IO.Directory.Exists((pathname + "ErrorLog\\" + System.DateTime.Now.Year.ToString() + "\\" + DateTime.Today.Month.ToString() + "\\")) == false)    
  35.         {    
  36.             System.IO.Directory.CreateDirectory((pathname + "ErrorLog\\" + System.DateTime.Now.Year.ToString() + "\\" + DateTime.Today.Month.ToString() + "\\"));    
  37.         }    
  38.         string ToaDate = DateTime.Today.Date.ToString("yyyyMMdd");    
  39.         string path = pathname + "ErrorLog\\" + System.DateTime.Now.Year.ToString() + "\\" + DateTime.Today.Month.ToString() + "\\";    
  40.         if (!File.Exists((path + "Log" + ToaDate + ".xml")))    
  41.         {    
  42.             File.Create((path + "Log" + ToaDate + ".xml")).Close();    
  43.     
  44.     
  45.             dt.Columns.Add("Date", System.Type.GetType("System.String"));    
  46.             dt.Columns.Add("Time", System.Type.GetType("System.String"));    
  47.             dt.Columns.Add("userid", System.Type.GetType("System.String"));    
  48.             dt.Columns.Add("userName", System.Type.GetType("System.String"));    
  49.             dt.Columns.Add("InnerExceptionType", System.Type.GetType("System.String"));    
  50.             dt.Columns.Add("InnerExceptionMessage", System.Type.GetType("System.String"));    
  51.             dt.Columns.Add("InnerExceptionSource", System.Type.GetType("System.String"));    
  52.             dt.Columns.Add("ErrorPage", System.Type.GetType("System.String"));    
  53.             dt.Columns.Add("ExceptionType", System.Type.GetType("System.String"));    
  54.             dt.Columns.Add("ExceptionMessage", System.Type.GetType("System.String"));    
  55.             dt.Columns.Add("TargetSite", System.Type.GetType("System.String"));    
  56.             dt.Columns.Add("StackTrace", System.Type.GetType("System.String"));    
  57.     
  58.             dt.TableName = "ErrorLog";    
  59.     
  60.             if (!string.IsNullOrEmpty(Convert.ToString(HttpContext.Current.Session["userid"])))    
  61.             {    
  62.                 struserid = Convert.ToString(HttpContext.Current.Session["userid"]);    
  63.                 strUsername = Convert.ToString(HttpContext.Current.Session["userName"]);    
  64.             }    
  65.             else    
  66.             {    
  67.                 struserid = "None";    
  68.                 strUsername = "None";    
  69.             }    
  70.             if (exc.InnerException != null)    
  71.             {    
  72.     
  73.                 strIET = exc.InnerException.GetType().ToString();    
  74.                 strIEM = exc.InnerException.Message.ToString();    
  75.                 strIES = exc.InnerException.Source.ToString();    
  76.             }    
  77.             else    
  78.             {    
  79.                 strIET = "None";    
  80.                 strIEM = "None";    
  81.                 strIES = "None";    
  82.             }    
  83.             if (exc.StackTrace != null)    
  84.             {    
  85.                 StrST = exc.Source.ToString();    
  86.             }    
  87.             else    
  88.             {    
  89.                 StrST = "None";    
  90.             }    
  91.     
  92.             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);    
  93.             dt.WriteXml((path + "Log" + ToaDate + ".xml"));    
  94.             dt = null;    
  95.         }    
  96.         else    
  97.         {    
  98.             ds.ReadXml((path + "Log" + ToaDate + ".xml"));    
  99.             if (ds.Tables.Contains("ErrorLog"))    
  100.             {    
  101.                 dt = ds.Tables["ErrorLog"];    
  102.                 if (dt.Rows.Count > 0)    
  103.                 {    
  104.                     if (!string.IsNullOrEmpty(Convert.ToString(HttpContext.Current.Session["userName"])))    
  105.                     {    
  106.                         struserid = Convert.ToString(HttpContext.Current.Session["userName"]);    
  107.                         strUsername = Convert.ToString(HttpContext.Current.Session["userName"]);    
  108.                     }    
  109.                     else    
  110.                     {    
  111.                         struserid = "None";    
  112.                         strUsername = "None";    
  113.                     }    
  114.                     if (exc.InnerException != null)    
  115.                     {    
  116.     
  117.                         strIET = exc.InnerException.GetType().ToString();    
  118.                         strIEM = exc.InnerException.Message.ToString();    
  119.                         strIES = exc.InnerException.Source.ToString();    
  120.                     }    
  121.                     else    
  122.                     {    
  123.                         strIET = "None";    
  124.                         strIEM = "None";    
  125.                         strIES = "None";    
  126.                     }    
  127.                     if (exc.StackTrace != null)    
  128.                     {    
  129.                         StrST = exc.Source.ToString();    
  130.                     }    
  131.                     else    
  132.                     {    
  133.                         StrST = "None";    
  134.                     }    
  135.     
  136.                     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);    
  137.                     dt.WriteXml((path + "Log" + ToaDate + ".xml"));    
  138.                 }    
  139.             }    
  140.             dt = null;    
  141.             ds = null;    
  142.         }    
  143.     }    
  144.     #endregion    
  145. }  
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.