Global.asax的Application_Error實現錯誤記錄/錯誤日志的代碼
更新時間:2013年08月14日 17:30:57 作者:
本文為大家介紹下利用Global.asax的Application_Error實現錯誤記錄,具體如下,有此需求的朋友可以參考下,希望對大家有所幫助
利用Global.asax的Application_Error實現錯誤記錄
錯誤日志
void Application_Error(object sender, EventArgs e)
{
// 在出現未處理的錯誤時運行的代碼
Exception ex = Server.GetLastError().GetBaseException();
StringBuilder str = new StringBuilder();
str.Append("\r\n" + DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"));
str.Append("\r\n.客戶信息:");
string ip = "";
if (Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR") != null)
{
ip = Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR").ToString().Trim();
}
else
{
ip = Request.ServerVariables.Get("Remote_Addr").ToString().Trim();
}
str.Append("\r\n\tIp:" + ip);
str.Append("\r\n\t瀏覽器:" + Request.Browser.Browser.ToString());
str.Append("\r\n\t瀏覽器版本:" + Request.Browser.MajorVersion.ToString());
str.Append("\r\n\t操作系統(tǒng):" + Request.Browser.Platform.ToString());
str.Append("\r\n.錯誤信息:");
str.Append("\r\n\t頁面:" + Request.Url.ToString());
str.Append("\r\n\t錯誤信息:" + ex.Message);
str.Append("\r\n\t錯誤源:" + ex.Source);
str.Append("\r\n\t異常方法:" + ex.TargetSite);
str.Append("\r\n\t堆棧信息:" + ex.StackTrace);
str.Append("\r\n--------------------------------------------------------------------------------------------------");
//創(chuàng)建路徑
string upLoadPath = Server.MapPath("~/log/");
if (!System.IO.Directory.Exists(upLoadPath))
{
System.IO.Directory.CreateDirectory(upLoadPath);
}
//創(chuàng)建文件 寫入錯誤
System.IO.File.AppendAllText(upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + ".log", str.ToString(), System.Text.Encoding.UTF8);
//處理完及時清理異常
Server.ClearError();
//跳轉至出錯頁面
Response.Redirect("~/error.html");
}
錯誤日志
復制代碼 代碼如下:
void Application_Error(object sender, EventArgs e)
{
// 在出現未處理的錯誤時運行的代碼
Exception ex = Server.GetLastError().GetBaseException();
StringBuilder str = new StringBuilder();
str.Append("\r\n" + DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"));
str.Append("\r\n.客戶信息:");
string ip = "";
if (Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR") != null)
{
ip = Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR").ToString().Trim();
}
else
{
ip = Request.ServerVariables.Get("Remote_Addr").ToString().Trim();
}
str.Append("\r\n\tIp:" + ip);
str.Append("\r\n\t瀏覽器:" + Request.Browser.Browser.ToString());
str.Append("\r\n\t瀏覽器版本:" + Request.Browser.MajorVersion.ToString());
str.Append("\r\n\t操作系統(tǒng):" + Request.Browser.Platform.ToString());
str.Append("\r\n.錯誤信息:");
str.Append("\r\n\t頁面:" + Request.Url.ToString());
str.Append("\r\n\t錯誤信息:" + ex.Message);
str.Append("\r\n\t錯誤源:" + ex.Source);
str.Append("\r\n\t異常方法:" + ex.TargetSite);
str.Append("\r\n\t堆棧信息:" + ex.StackTrace);
str.Append("\r\n--------------------------------------------------------------------------------------------------");
//創(chuàng)建路徑
string upLoadPath = Server.MapPath("~/log/");
if (!System.IO.Directory.Exists(upLoadPath))
{
System.IO.Directory.CreateDirectory(upLoadPath);
}
//創(chuàng)建文件 寫入錯誤
System.IO.File.AppendAllText(upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + ".log", str.ToString(), System.Text.Encoding.UTF8);
//處理完及時清理異常
Server.ClearError();
//跳轉至出錯頁面
Response.Redirect("~/error.html");
}
相關文章
EFCore 通過實體Model生成創(chuàng)建SQL Server數據庫表腳本
這篇文章主要介紹了EFCore 通過實體Model生成創(chuàng)建SQL Server數據庫表腳本的示例,幫助大家更好的理解和學習使用.net框架,感興趣的朋友可以了解下2021-03-03詳解ASP.NET-----Repeater數據控件的用法總結
本篇文章主要介紹了ASP.NET--Repeater數據控件的用法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。2016-11-11ASP.NET Core使用HostingStartup增強啟動操作方法詳解
這篇文章主要介紹了ASP.NET Core使用HostingStartup增強啟動操作,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11