Application_Error实现错误记录 Global.asax的Application_Error实现错误记录/错误日志的代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Application_Error实现错误记录 Global.asax的Application_Error实现错误记录/错误日志的代码

  2021-03-21 我要评论
想了解Global.asax的Application_Error实现错误记录/错误日志的代码的相关内容吗,在本文为您仔细讲解Application_Error实现错误记录的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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");
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们