捕獲ASP.NET程式發生的異常

來源:互聯網
上載者:User

捕獲ASP.NET異常,這種文章在網上已經屢見不鮮了,在這之前,我也看了不少別人寫的代碼,學了別人不少東西。在別人的基礎上,我添加了一些自己寫的東西,現在貼出來,共用一下,希望能對大家有點協助。

 1using System.Text;
 2using System.Web;
 3using System.Configuration;
 4using System.IO;
 5
 6namespace HNRInfo.Framework.HttpModule
 7{
 8    /**//// <summary>
 9    /// 捕獲未處理的系統的錯誤,將錯誤添加的資料庫中,同時指向一個友好的錯誤頁面
10    /// </summary>
11    public class CatchError : IHttpModule
12    {
13        private static string sysErrorPage = ConfigurationManager.AppSettings["SysErrorPage"].ToString();
14
15        public void Dispose()
16        {
17        }
18
19        public void Init(HttpApplication context)
20        {
21            context.Error += new EventHandler(SaveError);
22        }
23        //將捕獲的錯誤資訊插入資料庫
24        private void SaveError(object sender, EventArgs e)
25        {
26            HttpContext context = ((HttpApplication)sender).Context;
27            string path = context.Request.RawUrl;
28            string hostIP = context.Request.UserHostAddress;
29
30            string title = string.Empty;
31            string info = string.Empty;
32
33            GetLastError(context.Server.GetLastError(), ref title, ref info);
34
35            HNRInfo.Model.SystemError errorModel = new HNRInfo.Model.SystemError();
36            errorModel.errorTitle = title;
37            errorModel.errorInfo = info;
38            errorModel.occurUrl = path;
39            errorModel.hostIP = hostIP;
40
41            HNRInfo.DALFactory.PlatFactory.SystemError.Add(errorModel);
42
43            //重新導向到友好的錯誤頁面
44            context.Server.Transfer(sysErrorPage, false);
45        }
46
47        /**//// <summary>
48        /// 找到導致異常的最初錯誤
49        /// </summary>
50        private void GetLastError(Exception e, ref string title, ref string info)
51        {
52            if (e.InnerException != null)
53                GetLastError(e.InnerException, ref title, ref info);
54            else
55            {
56                title = e.Message;
57                info = e.StackTrace;
58            }
59        }
60    }
61}
62

35-41行,是存入資料庫的一些操作,這幾行代碼,不用理會。由於這段代碼的在一個名為HNRInfo.Framework的程式集中,故web.config中的配置為:

      <httpModules>
        <!--捕獲錯誤-->
        <add type="HNRInfo.Framework.HttpModule.CatchError, HNRInfo.Framework" name="HNRInfo.Framework" />
      </httpModules>

代碼沒有什麼,挺簡單的,簡單是因為.NET在後台已經為我們做了很多複雜的工作。我覺得大家如果有時間的話,可以看看這個StackTrace類型的用法,下邊列出一些朋友的文章(關於StackTrace的用法):
淺析StackTrace
使用 StackTrace 獲得更多跟 Exception 有關的資訊
用System.Diagnostices.StackTrace取得呼叫堆疊資訊。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.