December 2017 : Online courses in ASP.NET MVC and Angular 4. Conducted by Bipin Joshi. Read more...
Registration for December 2017 batches of ASP.NET MVC / Core and Angular 4 online courses have already started. Conducted by Bipin Joshi. Book your seat today ! Click here for more details.

Automate Error Logging using Custom Page Class

Introduction

For any webmaster keeping track of errors raised by the web application is an important job. The most common approach is to create a function that will log the exceptions in a text file. This function is then called in each and every web form. However, chance of some developer failing to call this function makes this approach error prone. Wouldn't it be better if somebody automatically keeps the log of page level exceptions for you? That's exactly this article is going to demonstrate.

The Page class

By default when you create a new web application in VS.NET all the web forms are inherited from a base class called System.Web.UI.Page. It is necessary that your code behind class inherits this Page class directly or indirectly. By creating your own custom class that inherits from Page class you can put all reusable code at one place. Then your web forms inherit from this custom instead of Page class so that the same functionality is readily available to them.

The custom page class for error logging

Whenever there is any unhandled exception in a web form ASP.NET raises an event called Error. This event is defined in the Page class. Normally you would write an event handler (Page_Error) that will handle this event and inside this event handler you will log the error to a text file. Instead of writing this event handler in each and every web form we will write in just once in a custom page class. Since our web forms will inherit from this custom page class they will automatically get the error logging functionality.

Let's create our own custom class that will inherit from System.Web.UI.Page class.

public class MyCustomPageClass:Page
{
protected override void OnError(EventArgs e)
{
base.OnError (e);
StreamWriter writer= 
File.AppendText(Server.MapPath("errorlog.txt"));
Exception ex=Server.GetLastError();
writer.WriteLine(ex.Message + "[" + DateTime.Now + "]");
writer.Close();
}
}

Here, we created a class called MyCustomPageClass that inherits from System.Web.UI.Page class. This class overrides a base class function called OnError. This function will actually acts as an event handler for the Error event. Inside this function we get the Exception object using Server.GetLastError() method. We open a text file in append mode using System.IO.File class and write the error message and time stamp into it.

Inheriting from the custom page class

Next we need to create a web form with code behind class that inherits from MyCustomPageClass.

public class WebForm1 : MyCustomPageClass
{
private void Page_Load(object sender, System.EventArgs e)
{
throw new Exception("My error message");
}
}

In the Page_Load event we simply throw a new exception so that we can test our base class. If you run the page you will find that a text file called ErroLog.txt gets created in your virtual folder with the error message ("My error message") written to it.

Summary

Inheriting from a custom page class is a powerful technique of code reuse in web forms. This approach not only saves you in terms of coding but also ensures that certain actions like error logging are performed without any developer intervention. You can of course extend the concept to suit your requirements.


Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 13 March 2005


Tags : ASP.NET Web Forms Administration