您现在的位置是:网站首页> 编程资料编程资料
Global.asax的Application_Error实现错误记录/错误日志的代码_实用技巧_
2023-05-24
342人已围观
简介 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操作系统:" + 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--------------------------------------------------------------------------------------------------");
//创建路径
string upLoadPath = Server.MapPath("~/log/");
if (!System.IO.Directory.Exists(upLoadPath))
{
System.IO.Directory.CreateDirectory(upLoadPath);
}
//创建文件 写入错误
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操作系统:" + 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--------------------------------------------------------------------------------------------------");
//创建路径
string upLoadPath = Server.MapPath("~/log/");
if (!System.IO.Directory.Exists(upLoadPath))
{
System.IO.Directory.CreateDirectory(upLoadPath);
}
//创建文件 写入错误
System.IO.File.AppendAllText(upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + ".log", str.ToString(), System.Text.Encoding.UTF8);
//处理完及时清理异常
Server.ClearError();
//跳转至出错页面
Response.Redirect("~/error.html");
}
您可能感兴趣的文章:
相关内容
- Global.asax的Application_BeginRequest实现url重写无后缀的代码_实用技巧_
- 详细说明asp.net中datareader 和 dataset 的区别_实用技巧_
- asp.net中GridView控件遍历的小例子_实用技巧_
- asp.net 通用的连接数据库实例代码_实用技巧_
- asp.net session的使用与过期实例代码_实用技巧_
- ASP.NET 广告控件AdRotator的使用方法与实例_实用技巧_
- Global.asax取物理路径/取绝对路径具体方法_实用技巧_
- asp.net运算符之逻辑运算符以及其他运算符介绍与实例_实用技巧_
- asp.net中url字符串编码乱码的原因与解决方法_实用技巧_
- asp.net中DBNull.Value,null,String.Empty区别浅析_实用技巧_
