Unhandled Exceptions with Global.asax

前端 未结 2 1879
时光取名叫无心
时光取名叫无心 2020-12-31 10:32

I am emailing unhandled exception details from global.asax. How can I get the path and/or filename of the aspx file or assembly file where an exception was not handled.

2条回答
  •  生来不讨喜
    2020-12-31 11:20

    If this is a ASP.NET application, which the tag suggest it is, you should be able to do something like this... The ctx.Request.Url.ToString() would give you the file name of where the error occurred.

    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");
    }
    

提交回复
热议问题