剛開始入門學習Asp.Net的網頁設計師經常會很快的上網去抓一些程式來套用自已本來就擁有的網站上面,沒有仔細的去測試的話,「Bug」存在是難免的,而且遇到一些比較神經大條的程式設計師(可能像我一樣XD),沒有去仔細檢查程式的話,就經常有使用者會說,「ㄟ~網站出問題了~~~~」工程師:「什麼問題呀?」使用者:「就是我用到一半點下去就出現錯誤呀。」工程師:「哪一個?」使用者:「忘了耶(搔頭~~~)」。就經常會有神經大條的工程師加上神經大條的使用者=處理不完的bug,那麼要怎麼樣子工程師知道是哪裡出了問題呢?你請使用者錯誤的時候寫下來~他八成會忘記,不如就靠我們24小時都不休息的應用程式吧!
剛開始入門學習Asp.Net的網頁設計師經常會很快的上網去抓一些程式來套用自已本來就擁有的網站上面,沒有仔細的去測試的話,「Bug」存在是難免的,而且遇到一些比較神經大條的程式設計師(可能像我一樣XD),沒有去仔細檢查程式的話,就經常有使用者會說,「ㄟ~網站出問題了~~~~」工程師:「什麼問題呀?」使用者:「就是我用到一半點下去就出現錯誤呀。」工程師:「哪一個?」使用者:「忘了耶(搔頭~~~)」。就經常會有神經大條的工程師加上神經大條的使用者=處理不完的bug,那麼要怎麼樣子工程師知道是哪裡出了問題呢?你請使用者錯誤的時候寫下來~他八成會忘記,不如就靠我們24小時都不休息的應用程式吧!
1.首先我們先建立一個新的檔案Global.asax(全域應用程式類別)






















public class Global : System.Web.HttpApplication




protected void Application_Start(object sender, EventArgs e)







protected void Session_Start(object sender, EventArgs e)







protected void Application_BeginRequest(object sender, EventArgs e)







protected void Application_AuthenticateRequest(object sender, EventArgs e)







protected void Application_Error(object sender, EventArgs e)



HttpApplication ctx = (HttpApplication)sender;

string url = ctx.Request.Url.ToString();//取得發生錯誤的網址

string SaveDate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();//取得年月日

using (System.IO.FileStream ds = new System.IO.FileStream(Request.PhysicalApplicationPath + "error\\er" + SaveDate + ".log", System.IO.FileMode.Append))//寫入error\er200979.log







Exception err = Server.GetLastError();


StringBuilder builder = new StringBuilder();

builder.Append(DateTime.Now.ToString());

builder.Append("\u0009");

builder.Append(url);

builder.Append("\u0009");

builder.Append(err.Source);

builder.Append("\u0009");

builder.Append(err.Message);

sw.WriteLine(builder);

sw.Flush();

sw.Close();



ds.Close();






protected void Session_End(object sender, EventArgs e)







protected void Application_End(object sender, EventArgs e)










我是以每一天的發生的錯誤來做一個log檔,而且會紀錄時間,網址。這樣的話才能用最快的速度來處理bug
2.記得建立error這個資料夾整理妥當
3.通常我會在後端在建立一個網頁是專門下載這些錯誤的訊息~不然的話就直接到資料夾下檢視錯誤訊息