asp.net 捕捉未处理的异常,并将异常信息保存到文件中

  1 <%@ Application Language="C#" %>
  2 
  3 <script runat="server">
  4 
  5     void Application_Start(object sender, EventArgs e) 
  6     {
  7         // 在应用程序启动时运行的代码
  8 
  9     }
 10     
 11     public  void Application_End(object sender, EventArgs e) 
 12     {
 13         // 在应用程序关闭时运行的代码
 14 
 15     }
 16 
 17     public void Application_Error(object sender, EventArgs e) 
 18     { 
 19        //  在出现未处理的错误时运行的代码 
 20         Exception objErr = Server.GetLastError().GetBaseException();
 21 
 22         string error = string.Empty;
 23         string errortime = string.Empty;
 24         string erroraddr = string.Empty;
 25         string errorinfo = string.Empty;
 26         string errorsource = string.Empty;
 27         string errortrace = string.Empty;
 28 
 29         error += "发生时间:" + System.DateTime.Now.ToString() + "<br>";
 30         errortime = "发生时间:" + System.DateTime.Now.ToString();
 31 
 32         error += "发生异常页: " + Request.Url.ToString() + "<br>";
 33         erroraddr = "发生异常页: " + Request.Url.ToString();
 34 
 35         error += "异常信息: " + objErr.Message + "<br>";
 36         errorinfo = "异常信息: " + objErr.Message;
 37 
 38         errorsource = "错误源:" + objErr.Source;
 39         errortrace = "堆栈信息:" + objErr.StackTrace;
 40         error += "----------------------------------------------------------------------------<br>";
 41         Server.ClearError();
 42         Application["error"] = error;
 43 
 44         //独占方式,因为文件只能由一个进程写入. 
 45         System.IO.StreamWriter writer = null;
 46         try
 47         {
 48             lock (this)
 49             {
 50                 // 写入日志 
 51                 string year = DateTime.Now.Year.ToString();
 52                 string month = DateTime.Now.Month.ToString();
 53                 string path = string.Empty;
 54                 string filename = DateTime.Now.Day.ToString() + ".txt";
 55                 path = Server.MapPath("~/Error/") + year + "/" + month;
 56                 //如果目录不存在则创建 
 57                 if (!System.IO.Directory.Exists(path))
 58                 {
 59                     System.IO.Directory.CreateDirectory(path);
 60                 }
 61                 System.IO.FileInfo file = new System.IO.FileInfo(path + "/" + filename);
 62                 //if (!file.Exists) 
 63                 //    file.Create(); 
 64                 //file.Open(System.IO.FileMode.Append);         
 65                 writer = new System.IO.StreamWriter(file.FullName, true);   //文件不存在就创建,true表示追加 
 66                 writer.WriteLine("用户IP:" + Request.UserHostAddress);
 67 
 68 
 69                 //if (Session["UserName"] != null) 
 70                 //{ 
 71                 //    writer.WriteLine("用户名" + System.Web.HttpContext.Current.Session["UserName"].ToString()); 
 72                 //} 
 73                 writer.WriteLine(errortime);
 74                 writer.WriteLine(erroraddr);
 75                 writer.WriteLine(errorinfo);
 76                 writer.WriteLine(errorsource);
 77                 writer.WriteLine(errortrace);
 78                 writer.WriteLine("--------------------------------------------------------------------------------------");
 79                 //writer.Close(); 
 80             }
 81         }
 82         finally
 83         {
 84             if (writer != null)
 85             {
 86                 writer.Close();
 87                 writer.Dispose();
 88             }
 89         }
 90         Response.Redirect("~/Error/errorPage.htm"); 
 91 
 92     }
 93 
 94     public  void Session_Start(object sender, EventArgs e) 
 95     {
 96 
 97     }
 98     
 99     
100 
101     public void Session_End(object sender, EventArgs e) 
102     {
103        
104 
105     }
106        
107 </script>

该文件内容在网上很容易找到,此处只是作为个人备用!

posted @ 2014-06-17 21:10  慕枫小屋  阅读(142)  评论(0)    收藏  举报