我们正在使用 asp.net 配置设置提供的自定义错误。在整个应用程序(PL/BLL/DAL)中,我们没有使用任何 try catch。因此,对于任何层应用程序中的任何异常,都将用户重定向到配置文件中自定义错误设置中设置的自定义错误页面。现在我们想在显示错误页面之前在日志文件中记录以下信息:
- Date & time
- Exception message & strack trace.
- Page Name
- Method Name
- Method Parameter & values.
请帮助我如何在自定义错误 page_load 事件中收集上述信息?
Thanks,
@Paul
您可以在会话中存储错误详细信息并在自定义错误页面中获取它们。
这段代码位于全局.asax:
protected void Application_Error(object sender, EventArgs e)
{
Exception err = Server.GetLastError();
Session.Add("LastError", err);
}
void Session_Start(object sender, EventArgs e)
{
Session["LastError"] = ""; //initialize the session
}
然后在你的错误页面 load:
protected void Page_Load(object sender, EventArgs e)
{
Exception err = Session["LastError"] as Exception;
//Exception err = Server.GetLastError();
if (err != null)
{
err = err.GetBaseException();
lblErrorMsg.Text = err.Message;
lblSource.Text = err.Source;
lblInnerEx.Text = (err.InnerException != null) ? err.InnerException.ToString() : "";
lblStackTrace.Text = err.StackTrace;
Session["LastError"] = null;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)