2013-08-19 70 views
0

我在123-Reg上托管一个网站,并且当前向用户显示我的错误消息 - 我要求他们在过去向我发送截图,以便调试正在发生的事情。如何通过电子邮件发送ASP.NET错误

但是,我想用一些文本设置一个自定义错误页面,然后通过电子邮件将错误发​​送到我的收件箱。然而,我在网上找到的任何东西似乎都建议对IIS进行更改。

由于我使用123-Reg进行托管,因此我无权访问IIS。这可以在应用程序级别上实现吗?

+0

如果你可以打开一个TCP套接字到端口25一个smtp服务器,你可以发送电子邮件。 –

+0

我有我的应用程序发送电子邮件。我只是不知道如何从应用程序定义自定义错误页面... –

+1

调查Elmah。 –

回答

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 = "[email protected]"; 
     const string FromAddress = "[email protected]"; 
     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"); 

    } 
0

您可以轻松地添加ELMAH:

  1. 右键单击您的项目,选择 “管理的NuGet包”
  2. 点击 “在线” - > “的NuGet官方包源”
  3. 类型ELMAH在搜索框中选择第一个匹配。点击安装。
  4. 启用通过查找以下行并改变它所以在你的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> 
    

快要大功告成:请确保您按照URL上面的说明以确保您的错误页面,并防止未经授权访问您的日志。

UPDATE我应该说上面只会显示你在Elmah仪表板上的错误。您可以配置它以发送电子邮件错误。你会发现在如何做到这一点这里一个例子:Send email from Elmah?

Elmah

相关问题