Global.asax的Application_Error實(shí)現(xiàn)錯(cuò)誤記錄/錯(cuò)誤日志的代碼
更新時(shí)間:2013年08月14日 17:30:57 作者:
本文為大家介紹下利用Global.asax的Application_Error實(shí)現(xiàn)錯(cuò)誤記錄,具體如下,有此需求的朋友可以參考下,希望對大家有所幫助
利用Global.asax的Application_Error實(shí)現(xiàn)錯(cuò)誤記錄
錯(cuò)誤日志
void Application_Error(object sender, EventArgs e)
{
// 在出現(xiàn)未處理的錯(cuò)誤時(shí)運(yùn)行的代碼
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.錯(cuò)誤信息:");
str.Append("\r\n\t頁面:" + Request.Url.ToString());
str.Append("\r\n\t錯(cuò)誤信息:" + ex.Message);
str.Append("\r\n\t錯(cuò)誤源:" + 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)建文件 寫入錯(cuò)誤
System.IO.File.AppendAllText(upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + ".log", str.ToString(), System.Text.Encoding.UTF8);
//處理完及時(shí)清理異常
Server.ClearError();
//跳轉(zhuǎn)至出錯(cuò)頁面
Response.Redirect("~/error.html");
}
錯(cuò)誤日志
復(fù)制代碼 代碼如下:
void Application_Error(object sender, EventArgs e)
{
// 在出現(xiàn)未處理的錯(cuò)誤時(shí)運(yùn)行的代碼
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.錯(cuò)誤信息:");
str.Append("\r\n\t頁面:" + Request.Url.ToString());
str.Append("\r\n\t錯(cuò)誤信息:" + ex.Message);
str.Append("\r\n\t錯(cuò)誤源:" + 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)建文件 寫入錯(cuò)誤
System.IO.File.AppendAllText(upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + ".log", str.ToString(), System.Text.Encoding.UTF8);
//處理完及時(shí)清理異常
Server.ClearError();
//跳轉(zhuǎn)至出錯(cuò)頁面
Response.Redirect("~/error.html");
}
您可能感興趣的文章:
- asp.net 在global中攔截404錯(cuò)誤的實(shí)現(xiàn)方法
- Global.cs中自動(dòng)獲取未處理的異常
- 在Global.asax文件里實(shí)現(xiàn)通用防SQL注入漏洞程序(適應(yīng)于post/get請求)
- Global.asax取絕對路徑的方法
- Global.asax取物理路徑/取絕對路徑具體方法
- Global.asax的Application_BeginRequest實(shí)現(xiàn)url重寫無后綴的代碼
- c#定時(shí)器和global實(shí)現(xiàn)自動(dòng)job示例
- ASP.net全局程序文件Global.asax用法分析
- ASP.NET中Global和URLReWrite用法
- 在C#中g(shù)lobal關(guān)鍵字的作用及其用法
相關(guān)文章
在阿里云函數(shù)計(jì)算上部署.NET Core 3.1的方法
這篇文章主要介紹了在阿里云函數(shù)計(jì)算上部署.NET Core 3.1的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07EFCore 通過實(shí)體Model生成創(chuàng)建SQL Server數(shù)據(jù)庫表腳本
這篇文章主要介紹了EFCore 通過實(shí)體Model生成創(chuàng)建SQL Server數(shù)據(jù)庫表腳本的示例,幫助大家更好的理解和學(xué)習(xí)使用.net框架,感興趣的朋友可以了解下2021-03-03asp.net下利用JS實(shí)現(xiàn)對后臺(tái)CS代碼的調(diào)用方法
asp.net下利用JS實(shí)現(xiàn)對后臺(tái)CS代碼的調(diào)用方法...2007-04-04詳解ASP.NET-----Repeater數(shù)據(jù)控件的用法總結(jié)
本篇文章主要介紹了ASP.NET--Repeater數(shù)據(jù)控件的用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。2016-11-11ASP.NET Core使用HostingStartup增強(qiáng)啟動(dòng)操作方法詳解
這篇文章主要介紹了ASP.NET Core使用HostingStartup增強(qiáng)啟動(dòng)操作,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11