Global.asax - Application_Error - 如何获取页面数据?

Ash*_*Ash 19 c# asp.net global-asax

我有这个代码:

using System.Configuration;

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError().GetBaseException();

    string ErrorMessage = ex.Message;
    string StackTrace = ex.StackTrace;
    string ExceptionType = ex.GetType().FullName;
    string UserId = Getloggedinuser();
    string WebErrorSendEmail =
       ConfigurationManager.AppSettings["WebErrorSendEmail"];

    // save the exception in DB
    LogStuffInDbAndSendEmailFromDb();
}
Run Code Online (Sandbox Code Playgroud)

这是(大部分)我的代码.在一小部分情况下,我没有得到足够的信息.我不知道异常起源于哪个页面.

如何获取与异常源自的页面相关的任何信息?

以下是最短消息的示例:

Base-64 char数组的长度无效.

在System.Web上的System.Web.UI.ObjectState.mat.UI.Object.UI.ObjectState.Formatter. System.Web.UI.HiddenFieldPageStatePersister.Load()中的.UI.Util.DeserializeWithAssert(IStateFormatter formatter,String serializedState)

Net*_*ity 24

您可以像这样获取当前请求的URL和页面:

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    if (HttpContext.Current != null)
    {
        var url = HttpContext.Current.Request.Url;
        var page = HttpContext.Current.Handler as System.Web.UI.Page;
    }
}
Run Code Online (Sandbox Code Playgroud)