天天看點

Application_Error應用

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; 

            errorsource = "錯誤源:" + objerr.source;

            errortrace = "堆棧資訊:" + objerr.stacktrace;

            error += "--------------------------------------<br>";

            server.clearerror();

            application["error"] = error;

            //獨占方式,因為檔案隻能由一個程序寫入.

           system.io.streamwriter writer=null;

            try

            {               

                lock (this)

                {

                    // 寫入日志

                    string year = datetime.now.year.tostring();

                    string month = datetime.now.month.tostring();

                    string path = string.empty;

                    string filename = datetime.now.day.tostring() + ".txt";

                    path = server.mappath("~/error/") + year + "/" + month;

                    //如果目錄不存在則建立

                    if (!system.io.directory.exists(path))

                    {

                        system.io.directory.createdirectory(path);

                    }

                    system.io.fileinfo file = new system.io.fileinfo(path + "/"+filename);        

                    //檔案不存在就建立,true表示追加

                    writer = new system.io.streamwriter(file.fullname, true)                   

                    writer.writeline("使用者ip:" + request.userhostaddress); 

                    writer.writeline(errortime);

                    writer.writeline(erroraddr);

                    writer.writeline(errorinfo);

                    writer.writeline(errorsource);

                    writer.writeline(errortrace); 

                }

            }

            finally 

            {

                if (writer != null)

                    writer.close();

            }    

            response.redirect("~/error/errorpage.aspx");

    }

errorpage.aspx

<head runat="server">

    <title>出錯資訊</title>

    <link href="css/site.css" type="text/css" rel="stylesheet" />   

</head>

<body>

    <form id="form1" runat="server">

        <asp:label id="label1" runat="server" width="568px"></asp:label>

    </form>

</body>

errorpage.aspx.cs

 protected void page_load(object sender, eventargs e)

    {

        this.label1.text = application["error"].tostring();

當然,錯誤頁你可以不顯示具體的錯誤資訊,而是給使用者一個友好的提示頁面。

原帖位址:http://blog.csdn.net/w809026418/archive/2009/08/19/4462237.aspx

繼續閱讀