错误处理程序

Global.asax代码: 

    void Application_Error(object sender, EventArgs e)  
    {  
        // 在出现未处理的错误时运行的代码 
         
            Exception objErr = Server.GetLastError().GetBaseException(); 
            string error = string.Empty; 
            string errortime = string.Empty; 
            string erroraddr = string.Empty; 
            string errorinfo = string.Empty; 
            string errorsource = string.Empty; 
            string errortrace = string.Empty;

            error += "发生时间:" + System.DateTime.Now.ToString() + "<br>"; 
            errortime = "发生时间:" + System.DateTime.Now.ToString();

            error += "发生异常页: " + Request.Url.ToString() + "<br>"; 
            erroraddr = "发生异常页: " + Request.Url.ToString();

            error += "异常信息: " + objErr.Message + "<br>"; 
            errorinfo = "异常信息: " + objErr.Message;

            //error +="错误源:"+objErr.Source+"<br>"; 
            //error += "堆栈信息:" + objErr.StackTrace + "<br>"; 
            errorsource = "错误源:" + objErr.Source; 
            errortrace = "堆栈信息:" + objErr.StackTrace; 
            error += "--------------------------------------<br>"; 
            Server.ClearError(); 
            Application["error"] = error;

            Common.Log.WriteLog(error);
            Response.Redirect("~/Error/ErrorPage.aspx");      

    }

ErrorPage.aspx.cs 
 protected void Page_Load(object sender, EventArgs e) 
    { 
        this.Label1.Text = Application["Error"].ToString(); 
    }

posted @ 2013-02-09 19:13  tinaleft  阅读(180)  评论(0编辑  收藏  举报