More Verbose Application_Error() handling in global.asx

Extracting exception specifics like line number, class, method, etc:

   /// <summary>
        /// Handles the Error event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Application_Error(object sender, EventArgs e)
                Logging logging = new Logging();

                Exception ex = Server.GetLastError().GetBaseException();

                StackTrace trace = new StackTrace(ex, true);                

                string sourceFile = trace.GetFrame(0).GetFileName();
                int sourceLine = trace.GetFrame(0).GetFileLineNumber();
                string sourceMethod = trace.GetFrame(0).GetMethod().Name;            
                string sourceClass = trace.GetFrame(0).GetMethod().DeclaringType.ToString();
                string sourceMessage = ex.Message.ToString();
                string sourceThreadID = AppDomain.GetCurrentThreadId().ToString();

                string currentUser = (SessionWrapper.CurrentLoggedInUser != null) ? SessionWrapper.CurrentLoggedInUser.UserId : "";

                logging.insertLog(sourceClass, DateTime.Now,
                    trace.ToString(), currentUser, "FATAL",
                    sourceLine.ToString(), sourceMessage, sourceMethod, sourceThreadID, 0, 0, currentUser, "EHAP");

            catch (Exception)
                // If logging to database fails, log to file system
                log.Fatal("EHAP Site Application_Error", Server.GetLastError());


            Response.Redirect("/SiteException.aspx", false);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s