项目中有时候会遇到这样子的错误,是在一定条件下出现,或者是偶尔出现的,不容易捕捉
这类错误,把下面的代码加到Global.asax,就能把项目中出的所以错误(加上Try除外)的
错误发送到邮箱里。
protected void Application_Error(object sender, EventArgs e)
{
string userHostAddress = "";
string userHostName = "";
string userAgent = "";
string str4 = "";
string absoluteUri = "";
string str6 = "";
string str7 = "";
string str8 = "";
string str9 = "";
string str10 = "";
string str11 = "";
Exception lastError = base.Server.GetLastError();
if (lastError.InnerException.ToString().Trim().IndexOf("System.IO.FileNotFoundException") >= 0)
{
if ((base.Request.Url.AbsoluteUri.Trim().IndexOf("localhost") < 0) && (base.Request.Url.AbsoluteUri.Trim().IndexOf("test.chetx.com") < 0))
{
base.Response.Write("<script language=javascript>alert('抱歉,您输入的页面地址有误或已不存在!');window.location.href('http://bbs.chetx.com/');</script>");
base.Response.End();
}
}
else
{
string str12 = "<style type='text/css'>body{font-size: 11pt;}</style>";
str12 = (str12 + "<b>时间</b>:" + DateTime.Now.ToString().Trim() + "<br>") + "<b>访问者信息</b>:<br>";
if (base.Request.UserHostAddress != null)
{
str12 = str12 + ""x00b7IP:" + base.Request.UserHostAddress + "<br>";
userHostAddress = base.Request.UserHostAddress;
}
if (base.Request.UserHostName != null)
{
str12 = str12 + ""x00b7DNS:" + base.Request.UserHostName + "<br>";
userHostName = base.Request.UserHostName;
}
if (base.Request.UserAgent != null)
{
str12 = str12 + ""x00b7代理:" + base.Request.UserAgent + "<br>";
userAgent = base.Request.UserAgent;
}
if (base.Request.UserLanguages != null)
{
str12 = str12 + ""x00b7语言:";
for (int i = 0; i < base.Request.UserLanguages.Length; i++)
{
str12 = str12 + base.Request.UserLanguages.GetValue(i).ToString().Trim();
str4 = str4 + base.Request.UserLanguages.GetValue(i).ToString().Trim();
if (i != (base.Request.UserLanguages.Length - 1))
{
str12 = str12 + " ";
str4 = str4 + " ";
}
else
{
str12 = str12 + "<br>";
}
}
}
str12 = str12 + "<b>错误信息</b>:<br>";
if (base.Request.UrlReferrer != null)
{
str12 = str12 + ""x00b7入口:" + base.Request.UrlReferrer.AbsoluteUri + "<br>";
if (base.Request.UrlReferrer.AbsoluteUri.LastIndexOf("?") > 0)
{
str12 = str12 + ""x00b7参数:" + base.Request.UrlReferrer.AbsoluteUri.Substring(base.Request.UrlReferrer.AbsoluteUri.LastIndexOf("?")) + "<br>";
}
absoluteUri = base.Request.UrlReferrer.AbsoluteUri;
}
if (base.Request.Url.AbsoluteUri != null)
{
string str13 = str12;
str12 = str13 + ""x00b7地址:<a href='" + base.Request.Url.AbsoluteUri + "' target='_blank'>打开</a> (" + base.Request.Url.AbsoluteUri + ")<br>";
if (base.Request.Url.AbsoluteUri.LastIndexOf("?") > 0)
{
str12 = str12 + ""x00b7参数:" + base.Request.Url.AbsoluteUri.Substring(base.Request.Url.AbsoluteUri.LastIndexOf("?")) + "<br>";
}
str6 = base.Request.Url.AbsoluteUri;
}
if (lastError.GetType() != null)
{
str12 = str12 + ""x00b7类型:" + lastError.GetType().ToString().Trim() + "<br>";
str7 = lastError.GetType().ToString().Trim();
}
if (lastError.InnerException != null)
{
if (lastError.InnerException.ToString().Trim().IndexOf("。"r"n") > 0)
{
int index = lastError.InnerException.ToString().Trim().IndexOf("。"r"n");
str12 = (str12 + ""x00b7信息:<font color='red'>" + lastError.InnerException.ToString().Trim().Substring(0, index) + "</font><br>") + ""x00b7源错:" + lastError.InnerException.ToString().Trim().Substring(index + 1) + "<br>";
str8 = lastError.InnerException.ToString().Trim().Substring(0, index);
}
else
{
str12 = str12 + ""x00b7信息:" + lastError.InnerException.ToString().Trim() + "<br>";
}
}
if (lastError.TargetSite != null)
{
str12 = str12 + ""x00b7方法:" + lastError.TargetSite.ToString().Trim() + "<br>";
str9 = lastError.TargetSite.ToString().Trim();
}
if (lastError.Source != null)
{
str12 = str12 + ""x00b7名称:" + lastError.Source.ToString().Trim() + "<br>";
str10 = lastError.Source.Trim();
}
if (lastError.Message != null)
{
str12 = str12 + ""x00b7描述:" + lastError.Message.ToString().Trim() + "<br>";
str11 = lastError.Message.ToString().Trim();
}
if (lastError.StackTrace != null)
{
str12 = str12 + ""x00b7堆栈:" + lastError.StackTrace.ToString().Trim() + "<br>";
}
if (lastError.HelpLink != null)
{
str12 = str12 + "<b>帮助</b>:" + lastError.HelpLink.ToString().Trim();
}
MailMessage message = new MailMessage();
message.Subject = base.Request.Url.Authority + "论坛出错!";
message.Body = str12;
message.To = "nie@***.com";
message.From = "nie@***.com";
SmtpMail.SmtpServer = "mail.***.com";
SmtpMail.Send(message);
}
}
这类错误,把下面的代码加到Global.asax,就能把项目中出的所以错误(加上Try除外)的
错误发送到邮箱里。
protected void Application_Error(object sender, EventArgs e)
{
string userHostAddress = "";
string userHostName = "";
string userAgent = "";
string str4 = "";
string absoluteUri = "";
string str6 = "";
string str7 = "";
string str8 = "";
string str9 = "";
string str10 = "";
string str11 = "";
Exception lastError = base.Server.GetLastError();
if (lastError.InnerException.ToString().Trim().IndexOf("System.IO.FileNotFoundException") >= 0)
{
if ((base.Request.Url.AbsoluteUri.Trim().IndexOf("localhost") < 0) && (base.Request.Url.AbsoluteUri.Trim().IndexOf("test.chetx.com") < 0))
{
base.Response.Write("<script language=javascript>alert('抱歉,您输入的页面地址有误或已不存在!');window.location.href('http://bbs.chetx.com/');</script>");
base.Response.End();
}
}
else
{
string str12 = "<style type='text/css'>body{font-size: 11pt;}</style>";
str12 = (str12 + "<b>时间</b>:" + DateTime.Now.ToString().Trim() + "<br>") + "<b>访问者信息</b>:<br>";
if (base.Request.UserHostAddress != null)
{
str12 = str12 + ""x00b7IP:" + base.Request.UserHostAddress + "<br>";
userHostAddress = base.Request.UserHostAddress;
}
if (base.Request.UserHostName != null)
{
str12 = str12 + ""x00b7DNS:" + base.Request.UserHostName + "<br>";
userHostName = base.Request.UserHostName;
}
if (base.Request.UserAgent != null)
{
str12 = str12 + ""x00b7代理:" + base.Request.UserAgent + "<br>";
userAgent = base.Request.UserAgent;
}
if (base.Request.UserLanguages != null)
{
str12 = str12 + ""x00b7语言:";
for (int i = 0; i < base.Request.UserLanguages.Length; i++)
{
str12 = str12 + base.Request.UserLanguages.GetValue(i).ToString().Trim();
str4 = str4 + base.Request.UserLanguages.GetValue(i).ToString().Trim();
if (i != (base.Request.UserLanguages.Length - 1))
{
str12 = str12 + " ";
str4 = str4 + " ";
}
else
{
str12 = str12 + "<br>";
}
}
}
str12 = str12 + "<b>错误信息</b>:<br>";
if (base.Request.UrlReferrer != null)
{
str12 = str12 + ""x00b7入口:" + base.Request.UrlReferrer.AbsoluteUri + "<br>";
if (base.Request.UrlReferrer.AbsoluteUri.LastIndexOf("?") > 0)
{
str12 = str12 + ""x00b7参数:" + base.Request.UrlReferrer.AbsoluteUri.Substring(base.Request.UrlReferrer.AbsoluteUri.LastIndexOf("?")) + "<br>";
}
absoluteUri = base.Request.UrlReferrer.AbsoluteUri;
}
if (base.Request.Url.AbsoluteUri != null)
{
string str13 = str12;
str12 = str13 + ""x00b7地址:<a href='" + base.Request.Url.AbsoluteUri + "' target='_blank'>打开</a> (" + base.Request.Url.AbsoluteUri + ")<br>";
if (base.Request.Url.AbsoluteUri.LastIndexOf("?") > 0)
{
str12 = str12 + ""x00b7参数:" + base.Request.Url.AbsoluteUri.Substring(base.Request.Url.AbsoluteUri.LastIndexOf("?")) + "<br>";
}
str6 = base.Request.Url.AbsoluteUri;
}
if (lastError.GetType() != null)
{
str12 = str12 + ""x00b7类型:" + lastError.GetType().ToString().Trim() + "<br>";
str7 = lastError.GetType().ToString().Trim();
}
if (lastError.InnerException != null)
{
if (lastError.InnerException.ToString().Trim().IndexOf("。"r"n") > 0)
{
int index = lastError.InnerException.ToString().Trim().IndexOf("。"r"n");
str12 = (str12 + ""x00b7信息:<font color='red'>" + lastError.InnerException.ToString().Trim().Substring(0, index) + "</font><br>") + ""x00b7源错:" + lastError.InnerException.ToString().Trim().Substring(index + 1) + "<br>";
str8 = lastError.InnerException.ToString().Trim().Substring(0, index);
}
else
{
str12 = str12 + ""x00b7信息:" + lastError.InnerException.ToString().Trim() + "<br>";
}
}
if (lastError.TargetSite != null)
{
str12 = str12 + ""x00b7方法:" + lastError.TargetSite.ToString().Trim() + "<br>";
str9 = lastError.TargetSite.ToString().Trim();
}
if (lastError.Source != null)
{
str12 = str12 + ""x00b7名称:" + lastError.Source.ToString().Trim() + "<br>";
str10 = lastError.Source.Trim();
}
if (lastError.Message != null)
{
str12 = str12 + ""x00b7描述:" + lastError.Message.ToString().Trim() + "<br>";
str11 = lastError.Message.ToString().Trim();
}
if (lastError.StackTrace != null)
{
str12 = str12 + ""x00b7堆栈:" + lastError.StackTrace.ToString().Trim() + "<br>";
}
if (lastError.HelpLink != null)
{
str12 = str12 + "<b>帮助</b>:" + lastError.HelpLink.ToString().Trim();
}
MailMessage message = new MailMessage();
message.Subject = base.Request.Url.Authority + "论坛出错!";
message.Body = str12;
message.To = "nie@***.com";
message.From = "nie@***.com";
SmtpMail.SmtpServer = "mail.***.com";
SmtpMail.Send(message);
}
}