Asp.net 4.0 : How to get exception details in custom error page?

痴心易碎 提交于 2019-12-22 05:05:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!