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

2

7

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

Bolte answered 11/7, 2012 at 3:15 Comment(0)
T
12

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;
        }
    }
Twinberry answered 5/8, 2012 at 7:57 Comment(0)
D
0

Set this attribute in customErrors section in web.config: redirectMode="ResponseRewrite"

Dealate answered 12/2, 2016 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.