asp.net頁面出錯時的處理方法總結

來源:互聯網
上載者:User

1.第一種做法,在Web.config檔案配置

<system.web>
      <customErrors defaultRedirect="~/ErrorPage.aspx" 
                     mode="RemoteOnly">
      </customErrors>
</system.web>

 

  defaultRedirect屬性用來指明當aspx頁面發生了未處理錯誤時導向的頁面; 但Asp.net使用重新導向機制來重新導航錯誤頁面,這樣錯誤資訊就會丟失,也就是說我們用Server.GetLastError()獲得的Exception對象始終是空的。雖然可以提示使用者出錯,並提供一個返回出錯頁面的連結,卻不能給管理員一個很好的錯誤提示。

2.第二種做法:在global檔案裡的Application_Error方法中處理

代碼

protected void Application_Error(Object sender, EventArgs e)
        {
            Exception ex=Server.GetLastError().GetBaseException();

            string errorTime="發生時間:"+DateTime.Now.ToString();
            string errorAddress="發生異常頁:"+Request.Url.ToString();
            string errorInfo="異常資訊:"+ex.Message;
            string errorSource="錯誤源:"+ex.Source;
            string errorTrace="堆棧資訊:"+ex.StackTrace;
            Server.ClearError();

            System.IO.StreamWriter writer=null;
            try
            {
                lock(this)
                {
                    //寫入日誌 
                    string year=DateTime.Now.Year.ToString();
                    string month=DateTime.Now.Month.ToString();
                    string day=DateTime.Now.Day.ToString();
                    string path=string.Empty;
                    string filename=DateTime.Now.ToString("yyyyMMdd")+".txt";
                    path=Server.MapPath("~/Error/")+year+month+day;
                    if(!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    System.IO.FileInfo file=new FileInfo(path+"/"+filename);
                    writer=new StreamWriter(file.FullName,true);//檔案不在則建立,true表示追加
                    writer.WriteLine("使用者IP:"+Request.UserHostAddress);
                    writer.WriteLine(errorTime);
                    writer.WriteLine(errorAddress);
                    writer.WriteLine(errorInfo);
                    writer.WriteLine(errorSource);
                    writer.WriteLine(errorTrace);
                    writer.WriteLine("-------------------------------------------------------");

                }
            }
            finally
            {
                if(writer!=null)
                {
                    writer.Close();
                }
            }
            Server.Transfer("~/ErrorPage.aspx"); //跳轉到顯示友好錯誤的頁面

        }

然後在ErrorPage.aspx頁面顯示一些好友的提示資訊.

3.第三種做法:在Page_Error事件裡面處理

代碼

        private void Page_Load(object sender, System.EventArgs e)
        {
            throw(new ArgumentNullException());
        }

        public void Page_Error(object sender,EventArgs e)
        {
            Exception ex=Server.GetLastError().GetBaseException();

            string errorTime="發生時間:"+DateTime.Now.ToString();
            string errorAddress="發生異常頁:"+Request.Url.ToString();
            string errorInfo="異常資訊:"+ex.Message;
            string errorSource="錯誤源:"+ex.Source;
            string errorTrace="堆棧資訊:"+ex.StackTrace;

            Server.ClearError();

            System.IO.StreamWriter writer=null;
            try
            {
                lock(this)
                {
                    //寫入日誌 
                    string year=DateTime.Now.Year.ToString();
                    string month=DateTime.Now.Month.ToString();
                    string day=DateTime.Now.Day.ToString();
                    string path=string.Empty;
                    string filename=DateTime.Now.ToString("yyyyMMdd")+".txt";
                    path=Server.MapPath("~/Error/")+year+month+day;
                    if(!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    System.IO.FileInfo file=new FileInfo(path+"/"+filename);
                    writer=new StreamWriter(file.FullName,true);//檔案不在則建立,true表示追加
                    writer.WriteLine("使用者IP:"+Request.UserHostAddress);
                    writer.WriteLine(errorTime);
                    writer.WriteLine(errorAddress);
                    writer.WriteLine(errorInfo);
                    writer.WriteLine(errorSource);
                    writer.WriteLine(errorTrace);
                    writer.WriteLine("-------------------------------------------");

                }
            }
            finally
            {
                if(writer!=null)
                {
                    writer.Close();
                }
            }

            Server.ClearError();//防止錯誤繼續到要被處理的 Application_Error 事件中。
            Response.Redirect("~/ErrorPage.aspx");
            
        }

 

我經常的做法是使用第二種方法,然後再寫一個傳送簡訊的方法(調用移動的簡訊借口),這樣的話程式出錯的時候,管理員可以收到程式出錯的資訊。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.