2 回答
TA贡献1828条经验 获得超4个赞
在全局文件 Global.asax 中做处理。截获出现异常时的事件,并自定义处理过程。
参考下面代码,增加一个日志的写入就ok了。
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
HttpContext context = ((HttpApplication)sender).Context;
Exception ex = context.Server.GetLastError();
if (ex == null || !(ex is HttpException) || (ex as HttpException).GetHttpCode() == 404)
return;
StringBuilder sb = new StringBuilder();
try
{
sb.Append("Url : " + context.Request.Url);
sb.Append(Environment.NewLine);
sb.Append(" Raw Url :" + context.Request.RawUrl);
sb.Append(Environment.NewLine);
while (ex != null)
{
sb.Append("Message : " + ex.Message);
sb.Append(Environment.NewLine);
sb.Append("Source : " + ex.Source);
sb.Append(Environment.NewLine);
sb.Append("StackTrace : " + ex.StackTrace);
sb.Append(Environment.NewLine);
sb.Append("TagetSite : " + ex.TargetSite);
sb.Append(Environment.NewLine);
ex = ex.InnerException;
}
}
catch (Exception ex2)
{
sb.Append("Error logging error : " + ex2.Message);
}
if (OASettings.Instance.EnableErrorLogging)
Utils.Log(sb.ToString());
context.Items["LastErrorDetails"] = sb.ToString();
context.Response.StatusCode = 500;
Server.ClearError();
context.Server.Transfer("~/error.aspx");
}
TA贡献1828条经验 获得超6个赞
- 2 回答
- 0 关注
- 463 浏览
添加回答
举报