1. 程式人生 > >只需要三步就可以建立錯誤日誌,記錄錯誤日誌,拿來就能用,無需改

只需要三步就可以建立錯誤日誌,記錄錯誤日誌,拿來就能用,無需改

第一步,在網站上建立一個資料夾,取名error(針對下面的程式碼只能用這個)

第二步,在網站上右鍵》新增新建項》全域性應用程式類  ,就是Global.asax(不能改名字,只能建立一次),

global檔案的 內容:(把這些全考過去就行了,其實改變的只是Application_Error裡面的程式碼,只把Application_Error裡面程式碼的拷過去也行)

<%@ Application Language="C#" %>


<script RunAt="server">


    void Application_Start(object sender, EventArgs e)
    {
        // 在應用程式啟動時執行的程式碼


    }


    void Application_End(object sender, EventArgs e)
    {
        // 在應用程式關閉時執行的程式碼


    }


    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;
        //獨佔方式,因為檔案只能由一個程序寫入.
        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);
                //if (!file.Exists)
                //    file.Create();
                //file.Open(System.IO.FileMode.Append);        
                writer = new System.IO.StreamWriter(file.FullName, true);//檔案不存在就建立,true表示追加
                writer.WriteLine("使用者IP:" + Request.UserHostAddress);
               // if (Session["Identity"] != null)
               // {
               //     writer.WriteLine("登入帳號:" System.Web.HttpContext.Current.Session["Identity"]).YongHuInfo.ACCOUNTID);
               // }
                writer.WriteLine(errortime);
                writer.WriteLine(erroraddr);
                writer.WriteLine(errorinfo);
                writer.WriteLine(errorsource);
                writer.WriteLine(errortrace);
                writer.WriteLine("【this Error From http://hi.baidu.com/yanwei99521】");
                writer.WriteLine("--------------------------------------------------------------------------------------");
            }
        }
        finally
        {
            if (writer != null)
                writer.Close();


        }
        Response.Redirect("~/HTMLPage.htm");
    }


    void Session_Start(object sender, EventArgs e)
    {
        // 在新會話啟動時執行的程式碼


    }


    void Session_End(object sender, EventArgs e)
    {
        // 在會話結束時執行的程式碼。 
        // 注意: 只有在 Web.config 檔案中的 sessionstate 模式設定為
        // InProc 時,才會引發 Session_End 事件。如果會話模式設定為 StateServer 
        // 或 SQLServer,則不會引發該事件。
        


    }
       
</script>

第三步,在網站下面新增一個htm檔案,做為錯誤頁面,取名HTMLPage.htm,也是針對上面的程式碼才取名叫這個的

測試結果

只要出錯就會蹦出錯誤頁面,如下:


然後可以在error資料夾裡面根據當前查詢錯誤日誌,根據錯誤日誌再修改程式碼,錯誤日誌是如下圖:


哦了~~這三部做完就大功告成了,快試一下吧