Global.asax捕获处理系统异常,插入日志表
protected void Application_Error(object sender, EventArgs e)
{
# region 系统异常处理
Exception ex = Server.GetLastError();//捕获应用程序的异常
while (ex.InnerException != null)
{
ex = ex.InnerException;
}
string strMessage = ex.Message;
string strStackTrace = ex.StackTrace;
string strUrl = Request.Url.ToString();
bool needExLog = bool.Parse(ConfigurationSettings.AppSettings["NeedExLog"]);
if (needExLog)
{
# region 记录异常日志
BMS_MMS.Model.Log_Exception model = new BMS_MMS.Model.Log_Exception();
model.PageUrl = strUrl;
model.Message = strMessage;
model.StackTrace = strStackTrace;
model.UserID = BMS_MMS.BLL.Sys_User.GetCurUser().ID.ToString();
model.LogDate = System.DateTime.Now;
BMS_MMS.BLL.Log_Exception bll = new BMS_MMS.BLL.Log_Exception();
bll.Insert(model);
# endregion
Response.Redirect("~/Error.aspx?ID=" + model.ID);
}
else
{
strMessage = Server.UrlEncode(strMessage);
Response.Redirect("~/Error.aspx?MSG=" + strMessage);
}
# endregion
}
本文介绍了一个基于Global.asax的应用程序错误处理机制,通过捕获系统异常并将其详细信息记录到日志表中,实现了有效的错误跟踪与管理。此外,还提供了根据配置选择是否记录异常的具体实现。
5910

被折叠的 条评论
为什么被折叠?



