Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 486
Paul Avatar asked Jul 11 '12 03:07

Paul


1 Answers

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;
        }
    }
like image 93
alisabzevari Avatar answered Sep 24 '22 20:09

alisabzevari