问题
We are using custom error provided by asp.net config setting. In entire application (PL/BLL/DAL) we are not using any try catch. So for any exception in any layer application redirect user to custom error page set in custom error setting in config file. Now we want to log following information in log file before showing error page:
- Date & time
- Exception message & strack trace.
- Page Name
- Method Name
- Method Parameter & values.
Please help me how to collect above information in custom error page_load event??
Thanks,
@Paul
回答1:
You can store error details in session and get them in custom error page.
This code is in Global.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
}
Then in your error page 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;
}
}
回答2:
Set this attribute in customErrors section in web.config: redirectMode="ResponseRewrite"
来源:https://stackoverflow.com/questions/11425032/asp-net-4-0-how-to-get-exception-details-in-custom-error-page