我正在123-Reg上托管一个网站,目前向用户显示我的错误消息 - 我已经要求他们过去给我发送截图,以便我可以调试正在发生的事情。
但是,我想设置一个带有一些文字的自定义错误页面,然后将错误通过电子邮件发送到我的收件箱。但是,我在网上发现的任何内容似乎都建议对IIS进行更改。
由于我使用123-Reg托管,因此无法访问IIS。这可以在应用程序级别实现吗?
答案 0 :(得分:0)
您可以轻松添加Elmah:
在web.config中启用“远程访问”,方法是找到以下行并将其更改为:
<elmah>
<!--
See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for
more information on remote access and securing ELMAH.
-->
<security allowRemoteAccess="true" />
</elmah>
您即将完成:请务必按照上述网址中的说明操作,以保护错误页面并防止未经授权访问您的日志。
更新我应该说上面只会向您显示Elmah信息中心的错误。您可以将其配置为还发送电子邮件错误。您将在此处找到有关如何执行此操作的示例:Send email from Elmah?
答案 1 :(得分:0)
在Global.asax页面上,只需添加以下代码
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
// Get the error details
HttpException lastErrorWrapper = Server.GetLastError() as HttpException;
Exception lastError = lastErrorWrapper;
if (lastErrorWrapper.InnerException != null)
lastError = lastErrorWrapper.InnerException;
string lastErrorTypeName = lastError.GetType().ToString();
string lastErrorMessage = lastError.Message;
string lastErrorStackTrace = lastError.StackTrace;
const string ToAddress = "youremail@yourdomain.com";
const string FromAddress = "noreply@yourdomain.com";
const string Subject = "An Error Has Occurred on Application Name!";
// Create the MailMessage object
MailMessage msg = new MailMessage();
string[] eAddresses = ToAddress.Split(';');
for (int i = 0; i < eAddresses.Length; i++)
{
if (eAddresses[i].Trim() != "")
{
msg.To.Add(new MailAddress(eAddresses[i]));
}
}
msg.From = (new MailAddress(FromAddress));
msg.Subject = Subject;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
msg.Body = string.Format(@"
<html>
<body>
<h1>An Error Has Occurred!</h1>
<table cellpadding=""5"" cellspacing=""0"" border=""1"">
<tr>
<td text-align: right;font-weight: bold"">URL:</td>
<td>{0}</td>
</tr>
<tr>
<td text-align: right;font-weight: bold"">User:</td>
<td>{1}</td>
</tr>
<tr>
<td text-align: right;font-weight: bold"">Exception Type:</td>
<td>{2}</td>
</tr>
<tr>
<td text-align: right;font-weight: bold"">Message:</td>
<td>{3}</td>
</tr>
<tr>
<td text-align: right;font-weight: bold"">Stack Trace:</td>
<td>{4}</td>
</tr>
</table>
</body>
</html>",
Request.Url,
Request.ServerVariables["LOGON_USER"],
lastErrorTypeName,
lastErrorMessage,
lastErrorStackTrace.Replace(Environment.NewLine, "<br />"));
// Attach the Yellow Screen of Death for this error
string YSODmarkup = lastErrorWrapper.GetHtmlErrorMessage();
if (!string.IsNullOrEmpty(YSODmarkup))
{
Attachment YSOD = Attachment.CreateAttachmentFromString(YSODmarkup, "YSOD.htm");
msg.Attachments.Add(YSOD);
}
// Send the email
SmtpClient smtp = new SmtpClient();
smtp.Send(msg);
Server.Transfer("/Error.aspx");
}