none
Using StackFrame class to get the previous exception's filename and line number?

    Question

  • Hi,

    I'm a little mind boggled. How can I use the StackFrame class to get the previous exceptions filename and line number? Right now, it is getting the FileInfo from my CustomError's page which users are being directed to , but I want to get the previous exception than log this info to the CustomError page. Here is my code:

    StackTrace

     

    st1 = new StackTrace(new StackFrame(1, true));

    Response.Write(st1);

    Thanks

    Monday, October 12, 2009 7:23 PM

Answers

  • Page with Error:

        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                try
                {
                    System.Text.StringBuilder a = new System.Text.StringBuilder();
                    a = null;
                    Response.Write(a.ToString());
                }
                catch (Exception ex)
                {
                    Session.Add("StackTrace", new System.Diagnostics.StackTrace(ex));
                    Response.Redirect("Webform2.aspx");
                }
            }
        }


    Error Page (Webform2):
        public partial class WebForm2 : System.Web.UI.Page
        {
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["StackTrace"] != null)
                    WriteOutError(this, Session["StackTrace"] as System.Diagnostics.StackTrace);
            }
    
            private static void WriteOutError(Page errorPage, System.Diagnostics.StackTrace st)
            {
                Array.ForEach<System.Diagnostics.StackFrame>(st.GetFrames(), errorPage.Response.Write);
            }
        }
    Monday, October 12, 2009 8:03 PM