Exception Handling in ASP.NET

Soldato
Joined
12 Jan 2004
Posts
6,824
Location
Londinium
Ive got an Application_Error method in my Global.asax file that looks like this:

Code:
protected void Application_Error(Object sender, EventArgs e)
{
	String Message = "\n\nThe Commission Menu Calculator has encountered the following error:"
		+ "\n\nURL:\n " + Request.Url
		+ "\n\nMessage:\n " + Server.GetLastError().Message
		+ "\n\nSource:\n " + Server.GetLastError().Source
		+ "\n\nHelp Link:\n " + Server.GetLastError().HelpLink
		+ "\n\nStack Trace:\n" + Server.GetLastError().StackTrace;

	// Create event Log if it does not exist
	String LogName = "Application";
	if (!EventLog.SourceExists(LogName))
		EventLog.CreateEventSource(LogName, LogName);

	// Insert into event log
	EventLog Log = new EventLog();
	Log.Source = LogName;
	Log.WriteEntry(Message, EventLogEntryType.Error);
}

It works fine, but the trouble is, the information that gets written to the event log is no where near as useful as the information that gets displayed on the default "Server Error in '/' Application" error page. Here is a comparison:

"Server Error in '/' Application" error page:

Code:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.
Source File: C:\Inetpub\ComCalc\ComCalc.aspx    Line: 8

Event Log:

Code:
URL:
 http://fsasys05/ComCalc.aspx

Message:
 Exception of type System.Web.HttpUnhandledException was thrown.

Source:
 System.Web

Help Link:
 

Stack Trace:
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain()
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously).

As you can see, the event log is pretty useless compared to the default error page.

Is there any way to get the detailed info on the default error page into the event log?
 
Could be wrong, but you may be wanting the InnerException property of the exception. Looks like in your example the IndexOutOfRangeException is the innerexception to the main exception. Problem is, because the innerexception is of type exception, that too could have an innerexception. Depends how far you want to go down I suppose...
 
Back
Top Bottom