Back to "Catching all errors in an ASP.NET application"

This is a viewer only at the moment see the article on how this works.

To update the preview hit Ctrl-Alt-R (or ⌘-Alt-R on Mac) or Enter to refresh. The Save icon lets you save the markdown file to disk

This is a preview from the server running through my markdig pipeline

ASP.NET C# Imported mostlylucidcouk Software Development

Catching all errors in an ASP.NET application

Wednesday, 12 May 2004

Okay, I realise that probably everyone and his / her mother knows this already, but if you want to log ALL errors in an ASP.NET application, the simplest way I've found to do it is in the Global.asax.cs (or whatever codebehind)...simply do this:


protected void Application_OnError(object sender, EventArgs e)
	{
		Exception ex = Server.GetLastError();
		if ( ex is HttpException )
		{
			if (((HttpException)ex).GetHttpCode() == 404)
				return;
		}     
		ExceptionManager.Publish(ex);

	}

In this instance, ExceptionManager is the Microsoft Exception Manager application block...you can (and really should) use this in conjunction with CustomError. The reason I do it here is that I prefer to make my error page flat html...just in case something really bad has happened and an active error page would cause an error itself :-) Just thought, I'll post up some code which emails you errors using the EMAB...

logo

© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.