Global.asax文件没有给出异常消息

时间:2010-09-28 11:26:31

标签: asp.net email global-asax

这是我的问题我有一些代码,我用它发送一封电子邮件,其中包含最后的错误详细信息,但我想要的是(内部)异常消息将显示在带有URL

这是我的代码

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

' Get current exception 
    Dim err As System.Exception = Server.GetLastError

    Dim ErrorDetails As String = err.Exception.Message

    Dim ErrorURL As String = Request.Url.ToString()

    ' Send notification e-mail
    Dim Email As MailMessage = _
        New MailMessage("email@email.co.uk", email@email.co.uk")
    Email.IsBodyHtml = False
    Email.Subject = "WEB SITE ERROR"
    Email.Body = ErrorDetails & vbcrlf & vbcrlf & ErrorURL
    Email.Priority = MailPriority.High
    Dim sc As SmtpClient = New SmtpClient("localhost")
    sc.Send(Email)

End Sub

非常感谢任何帮助

由于

杰米

2 个答案:

答案 0 :(得分:3)

使用err.ToString() - 这将为您提供完整的堆栈跟踪和内部异常。

如果您真的只想要内部异常错误消息,请使用err.InnerException.Message

答案 1 :(得分:1)

protected void Application_Error(object sender, EventArgs e)

{

MailMessage msg = new MailMessage();
HttpContext ctx = HttpContext.Current;

msg.To.Add(new MailAddress("me@me.com"));
msg.From = new MailAddress("from@me.com");
msg.Subject = "My app had an issue...";
msg.Priority = MailPriority.High;

StringBuilder sb = new StringBuilder();
sb.Append(ctx.Request.Url.ToString() + System.Environment.NewLine);
sb.Append("Source:" + System.Environment.NewLine + ctx.Server.GetLastError().Source.ToString());
sb.Append("Message:" + System.Environment.NewLine + ctx.Server.GetLastError().Message.ToString());
sb.Append("Stack Trace:" + System.Environment.NewLine + ctx.Server.GetLastError().StackTrace.ToString());
msg.Body = sb.ToString();

//CONFIGURE SMTP OBJECT
SmtpClient smtp = new SmtpClient("myhost");

//SEND EMAIL
smtp.Send(msg);

//REDIRECT USER TO ERROR PAGE
Server.Transfer("~/ErrorPage.aspx");
}
相关问题