分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

.net中的Global的错误日志

发布时间:2023-09-06 01:21责任编辑:郭大石关键词:错误日志

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"); 

.net中的Global的错误日志

原文地址:http://www.cnblogs.com/jerry123/p/7762980.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved