Hi,
One of the most critical aspect of and asp.net application is exception handling. When an application is there live on the web there can be an exception happening. These exception needs to be logged so that they can be rectified later.
There are many inbuilt ay in which this can be done. Like we can handle the exception at page level, or at the application level or even better create a simple reusable module that can be used in multiple applications.
In this post I will show how we can log the exception in an application. There are three prominent places where we can log the exception.
1 In a text file (or in XML format)
2 In a Database (I know text file is also a database but I meant a RDBMS)
3 Send the details in a Mail.
I would advise to log the exception in all the three places. We never know the exception might have caused because of failure of mail system, or database or problem with writing in the file system. Even if one of them fails the exception will be logged at one place for sure.
As this is a common to many applications it would be good to create a module with this functionality that can be added to any web application. So I decided to create a HttpModule that logs the exception.
To create an HttpModule we need to create a class that implements the IHttpModule interface. To implement the IHttpModule we need to implement 2 methods, dispose and Init. For this module we do not have anything to clean in the dispose method.
In the Init method we need to create a new handler for the error event of the application. Now whenever an exception is thrown by the application this method will be called.
void IHttpModule.Init(HttpApplication context)
{
context.Error += new EventHandler(context_Error);
}
In the context_Error method we need to write the logic for logging the details of the exception
void context_Error(object sender, EventArgs e)
{
// Get the Last Error in the system
Exception ex = HttpContext.Current.Server.GetLastError();
string FileBeginPath = HttpContext.Current.Server.MapPath("") + @"\SomeName";
#region Logic to Save the Exception in a textFile
try
{
StreamWriter sw = File.CreateText(FileBeginPath + DateTime.Now.Ticks.ToString() + ".txt");
sw.WriteLine(ex.StackTrace.ToString()); // write more stuff in the file
sw.Close();
}
catch
{ }
#endregion
#region Logic to Save the Exception detail in a Database
// TODO:
#endregion
#region Logic to Send the Exception detail in a Mail
// TODO:
#endregion
}
Here I am logging the exception in a text file that will be saved in the root of the server. I am also writing the StackTrace of exception in the file. [Note: Normally we should not be saving the details in the root with only the StackTrace. This has been done only for the example purpose. Also we can take the connection string for logging, mailing details and path for saving text file for the web.config file.] I have also left space for logic of writing the exception in the database and sending the mail of the exception.
Note that its important that we catch any exception that might be caused during the logging of exception. You never know the problem might just be with file writing permission, or database connection. If an exception is thrown by the logging the logic then the really error might never be logged :-).
To use the given module in any web application we need to register the module in the web.config like this.
<httpModules>
<add type="ErrorLoggingModule, MyAppName" name="ErrorLoggingModule"/>
</httpModules>
Vikram