天天看点

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

继续阅读