Global Error Handler – C#.NET example
This is the same as the VB.NET global error handler example, but using C#
Global.asax
<%@ Application Language="C#" %>
<script RunAt="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
HttpContext ctx = HttpContext.Current;
Exception exception = ctx.Server.GetLastError ();
// do not redirect for 404
if (exception.GetType().FullName == "System.Web.HttpException")
{
HttpException exHttp = (HttpException)exception;
if (exHttp.GetHttpCode() == 404) throw exHttp;
}
ctx.Items["LastError"] = exception;
ctx.Server.ClearError ();
ctx.Server.Transfer("ErrorHandler.aspx");
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
ErrorHandler.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ErrorHandler.aspx.cs" Inherits="ErrorHandler" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>"> <html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>" > <head runat="server"> <title><%= ConfigurationManager.AppSettings["ApplicationName"] %> -- Error Page</title> <link href="ReportsStyle.css" type="text/css" rel="Stylesheet" /> </head> <body> <form id="form1" runat="server"> <asp:Panel ID="panel1" runat="server"> <h2>An error occurred.</h2> <p><asp:Label ID="lblUserMsg" runat="server" /></p> <hr /> <p><asp:Label ID="lblErrType" runat="server" Text="<b>Type:</b> " /></p> <p><asp:Label ID="lblErrMsg" runat="server" text="<b>Message:</b> " /></p> <p><asp:Label ID="lblErrDetail" runat="server" text="<b>Detail:</b> " /></p> <p><asp:Label ID="lblTrace" runat="server" Text="<b>Trace:</b> " /></p> <p><asp:Label ID="lblErrSrc" runat="server" Text="<b>Source:</b> " /></p> </asp:Panel> </form> </body> </html>
Code behind
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ErrorHandler : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/* application_onerror redirects here; if there is any compilation error,
* you get an infinite loop.
* SO BE CAREFUL CHANGING THIS PAGE
*/
Exception lastException, baseException;
try
{
lastException = (Exception)Context.Items["LastError"];
string detail = (string)Context.Items["ErrorDetail"];
if (lastException != null)
{
baseException = lastException.GetBaseException();
lblErrType.Text += baseException.GetType().ToString();
lblErrMsg.Text += baseException.Message;
if (detail != null) lblErrDetail.Text += detail;
lblTrace.Text += baseException.StackTrace;
lblErrSrc.Text += baseException.Source;
}
else
{
lblErrType.Text += "Not available";
lblErrMsg.Text += "Not available";
lblTrace.Text += "Not available";
lblErrSrc.Text += "Not available";
}
}
catch (Exception exception)
{
//mostly this is here in case we did something stupid in that try block up there.
lblErrMsg.Text = "Error Message: " + exception.Message;
lblTrace.Text = "Stack Trace: " + exception.StackTrace;
lblErrSrc.Text = "Error Source: " + exception.Source;
}
}
}
It has the same caveats I mentioned in the VB post. Precompile it, or you may find yourself in an endless loop.
Happy coding!
Tags: ASP.NET 2.0, C#.NET 2.0, global.asax
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.