【问题标题】:Asp.net 4.0 : How to get exception details in custom error page?Asp.net 4.0:如何在自定义错误页面中获取异常详细信息?
【发布时间】:2012-07-11 03:15:54
【问题描述】:

我们正在使用 asp.net 配置设置提供的自定义错误。在整个应用程序(PL/BLL/DAL)中,我们没有使用任何 try catch。因此,对于任何层应用程序中的任何异常,都将用户重定向到配置文件中自定义错误设置中设置的自定义错误页面。现在我们想在显示错误页面之前在日志文件中记录以下信息:

- Date & time
- Exception message & strack trace.
- Page Name
- Method Name
- Method Parameter & values.

请帮我如何在自定义错误page_load事件中收集上述信息??

谢谢,

@保罗

【问题讨论】:

    标签: asp.net-4.0 custom-error-pages exception-logging


    【解决方案1】:

    您可以在会话中存储错误详细信息并在自定义错误页面中获取它们。

    此代码在 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
        }
    

    然后在您的错误页面加载:

        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】:

      在 web.config 的 customErrors 部分设置此属性:redirectMode="ResponseRewrite"

      【讨论】:

        猜你喜欢
        • 2011-01-28
        • 2013-11-13
        • 2022-11-20
        • 1970-01-01
        • 1970-01-01
        • 2013-07-25
        • 2013-03-31
        • 2019-03-27
        • 1970-01-01
        相关资源
        最近更新 更多