冒泡显示来自Global.asax的全栈跟踪错误

本文关键字:全栈 跟踪 错误 asax Global 显示 | 更新日期: 2023-09-27 18:06:13

我正在实现一个自定义页面,我现在所拥有的并不像我想象的那样工作。我如何修复下面的代码以冒泡显示完整的堆栈跟踪?

我正在使用Application_Error。

谢谢!

EventLog eventLog = new EventLog();
            //Write an error to event log
            eventLog.LogException(
                String.Format("UNHANDLED EXCEPTION'nPage: {0}", ((Global)(sender)).Context.Request.Path),
                ex);
            HttpContext httpContext = HttpContext.Current;
           // Exception exception = httpContext.Server.GetLastError();
            //build out custom formatted error page
            string strError = "<html>" +
                "<head runat='server'>" +
                "<title>Support Error Page</title>" +
                "<link href='Styles/errorPage.css' rel='stylesheet' type='text/css' media='screen' />" +
                 "<script src='js/jquery.min.js' type='text/javascript'></script>" +
                "<script src='js/expand.js' type='text/javascript'></script>" +
                "<script type='text/javascript'>" +
                    "$(function() {" +
                        "$('#outer  h2.expand').toggler({method: 'slideFadeToggle'});" +
                    "});" +
                "</script>" +
                "</head>" +
                "<body>" +
                    "<form id='form1' runat='server'>" +                       
                            "<div id='wrapper'> " +
                                 "<div id='content'>" +
                                    "<h1 class='heading'>An error occurred processing this request</h1>" +
                                      "<div id='outer'>" +
                                        "<h2 class='expand'>Click here to expand/collapse the error</h2>" +
                                        "<div class='collapse'>" +
                                            "<p><b>Error occurred in page: </b>" + httpContext.Server.GetLastError().Source.ToString() + "</p>" +
                                            "<p><b>Error Message: </b>" + httpContext.Server.GetLastError().Message.ToString() + "</p>" +
                                            "<p><b>Stack Trace: </b>" + httpContext.Server.GetLastError().StackTrace.ToString() + "</p>" +
                                        "</div>" +
                                      "</div>" +
                                    "</div>" +
                                "</div>" +                             
                     "</form>" +
                    "</body>" +
                   "</html>";
            httpContext.Response.Write(strError);
            //  clear the error
            httpContext.Server.ClearError();
        }
    }

冒泡显示来自Global.asax的全栈跟踪错误

如果您的意思是在HTML呈现中显示完整的堆栈跟踪,则可能取决于如何在应用程序中捕获和抛出异常。如果您在应用程序的其他地方处理异常,并且在这些实例中捕获它们,则抛出一个新的异常,使用httpContext.Server.GetLastError()。InnerException对于获得底层堆栈跟踪可能很有用。

当捕获并重新抛出异常时,如果构造一个新的异常实例并将捕获的异常传递给新实例的构造函数,则在全局错误处理程序中可以使用该上下文。

见http://msdn.microsoft.com/en-us/library/system.exception.innerexception.aspx