所以我有一個全局錯誤處理程序,每當我的應用程序出現錯誤時都會發送一封電子郵件。這很好,但是,有一些錯誤(即可怕的「對象引用未設置爲對象實例」錯誤),這些錯誤非常難以追蹤。我有一個整潔的函數,可以將序列化模型轉換爲字符串,因此我可以實際看到有問題的代碼行處理的數據,但是我想知道是否有方法可以在應用程序中捕獲模型級錯誤處理程序?我得到了我需要的所有東西,例如url,堆棧跟蹤等,只是對當前正在處理的數據沒有可見性。這是我目前在我的Application_Error:在Application_Error中獲取當前模型
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
HttpContext con = HttpContext.Current;
string serverUrl = "";
if (con.Request != null)
{
serverUrl = con.Request.Url.ToString();
}
var user = "";
if (GlobalSiteData.CurrentUser != null) user = GlobalSiteData.CurrentUser.Username;
var ip = (Request.UserHostAddress == null ? "" : Request.UserHostAddress.ToString());
try {
Mailers.UserMailer mailer = new Mailers.UserMailer();
ErrorLog elog = new ErrorLog
{
ErrorDate = DateTime.Now,
InnerException = (exception.InnerException == null ? "" : exception.InnerException.Message),
Message = exception.Message,
UserName = user,
Source = exception.TargetSite.Name,
SourceUrl = serverUrl,
StackTrace = exception.StackTrace,
IPAddr = ip
};
mailer.LogEntry(elog).Send();
ErrorHelpers.LogErrorToFile(elog);
}
catch (Exception ex)
{
string err = ex.Message;
}
}