Being an ASP.NET developer, we develop a web project and get it completed in a months time as there are many organizations which work on very tight bound timelines for developing web sites or web application projects and deliver the project to the client in months.
While after project delivery, if client comes up saying that they are facing problems or that the application is giving errors, that time it always becomes a headache for a developer to test each thing even whole project sometimes to find smaller errors (by means of error here, we are not talking about logical errors, we are talking about exceptions which are mostly responsible for errors in the application). There are many exceptions that the .NET Framework fires which we never come to find out while debugging or developing the application. Those exceptions never directlyaffect the application but each small exception puts the unnecessary load on the server.
Here is the solution to track each and every small thing happening inside your ASP.NET web application, which I named as “Custom error tracking library”.
Custom error tracking library
The very first question that will come is "what is the core concept for this?"
Concept is like we will be maintaining an XML file with a predefined structure which I have built which will contain all the required fields as nodes and subs which we call as errorlog.xml.
There will be a CS library file which will track each and every minor error/exception that happens inside the application or .NET Framework and will put the error details inside the XML file.
Once we got the CS library ready with us, we just have a simple function with 2 overload methods as per the requirement in each
catch block of the code and also in
Application_Error event of Global.asax file.
XML File (errorlog.xml)
<xml version="1.0″ encoding="utf-8″>
<errordetails>Details goes here</errordetails>
Root node of XLM file will be inside root node. There will be a sub node which will get duplicated for each error. For each error, library will generate a node with all the below listed details in XML file. Next to last error node. So for each error, there will be a separate ERROR node.
Each field of the above XML file is descibed below:
Datetime: Date and time of the error/exception
File name: File name where exception or error happens
Class name : classname in which error occurred
Methodname: Function name where error occurred
Errormethod: Name of the function which contains error code.
Message: Error message/exception message
Error details: Detailed error description which will show whole stack trace of the functional execution.
IP: Client IP address
URL: Absolute error URL
There will be a CS library file where we will write all functionality for error handling and writing errors into XML file.
errorHamdler.cs file will have 2
static methods named
WriteError(), there will be 2 overloaded methods for same functions with different parameters.
CS file will look as given below. We name it as errorHamdler.cs.
public class errorHandler
string _strErrorMessage, _strDetails, _strClassName, _strMethodName;
DateTime _dtOccuranceTime = new DateTime();
public errorHandler(DateTime time, string className, string methodName,
string errorMessage, string details)
_dtOccuranceTime = time;
_strClassName = className;
_strDetails = details;
_strErrorMessage = errorMessage;
_strMethodName = methodName;
public static void WriteError(Exception ex)
public static void WriteError(Exception ex, string fileName)
XmlDocument doc = new XmlDocument();
string strRootPath =
string xmlPath = System.Web.HttpContext.Current.Server.MapPath(strRootPath);
XmlNode newXMLNode, oldXMLNode;
oldXMLNode = doc.ChildNodes.ChildNodes;
newXMLNode = oldXMLNode.CloneNode(true);
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();
newXMLNode.ChildNodes.InnerText = DateTime.Now.ToString();
newXMLNode.ChildNodes.InnerText = fileName;
newXMLNode.ChildNodes.InnerText = methodBase.DeclaringType.FullName;
newXMLNode.ChildNodes.InnerText = methodBase.Name;
newXMLNode.ChildNodes.InnerText = ex.TargetSite.Name;
newXMLNode.ChildNodes.InnerText = ex.Message;
newXMLNode.ChildNodes.InnerText = ex.StackTrace;
In the above code, there is a line:
string strRootPath = System.Configuration.ConfigurationManager.AppSettings["logfilepath"].ToString();
We need to give XML file path also where we have placed XML file in the project, so just have to add 1 line in web.config file as given below to store the actual XML file path, which will be used in the above function.
Code Inside web.config
<add key="logfilepath" value="~/errorHandling/errorlog.xml"/>
How to Use Error Handler in Application
Now everything is ready to be used in real time application. In each
catch block, we have to call
Writeerror() function as described below.
Code Inside Default.aspx.vb
protected void Page_Load(object sender, EventArgs e)
throw new Exception("Custom error");
catch (Exception ex)
Apart from each
catch block, we will put some code in Global.asax file also, as given below:
void Application_Error(object sender, EventArgs e)
The reason behind putting code in Global.asax file is very important and necessary as there might be many exceptions which occur at the application level and cannot be traceable in any
catch blocks in any function or events. So any error except
catch blocks will come in this event (
Application_Error) and will get inserted into XML file.
That’s it, we are done with the error handling, and all errors will be tractable from the XML file which is being generated via the error handler.