ASP.NET Core 5.0 : MVC, Razor Pages, Web API, EF Core, Blazor, Design Patterns, and more. Private online coaching for software developers. Click here for more details.

Automate Error Logging using Custom Page Class


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= 
Exception ex=Server.GetLastError();
writer.WriteLine(ex.Message + "[" + DateTime.Now + "]");

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.


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 an independent software consultant, trainer, author, yoga mentor, and meditation teacher. He has been programming, meditating, and teaching for 24+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. 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 Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 13 March 2005

Tags : ASP.NET Web Forms Administration

Subscribe to our newsletter

Get monthly email updates about new articles, tutorials, code samples, and how-tos getting added to our knowledge base.


Receive Weekly Updates