Global.asax的Application_Error实现错误记录/错误日志的代码
程序员文章站
2024-02-28 15:48:04
利用global.asax的application_error实现错误记录 错误日志 复制代码 代码如下: void application_error(object se...
利用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");
}