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

我有这个代码:

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(); } 

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

如何获得与exception源自的页面相关的任何信息?

以下是最短消息的示例:

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

System.Web上的System.Web.UI.ObjectState.mat.UI.Object.UI上的System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState)的System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)处的System.Convert.FromBase64String(String s)处的System.Convert.FromBase64String(String s) System.Web.UI.HiddenFieldPageStatePersister.Load()中的.UI.Util.DeserializeWithAssert(IStateFormatter formatter,String serializedState)

您可以像这样获取当前请求的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; } }