Asp.net:意外错误处理 - 发送邮件并重定向到error.aspx?

时间:2011-05-05 08:23:11

标签: asp.net error-handling

在我非常大的网页上(50页,20.000行代码)我只有极少数情况可能出现意外错误,我无法正确捕捉。

但是为了我的安全: 是否有可能,向我发送一个电子邮件(SMTP-Class已经存在,所以我只需要调用一个方法)并将每个未发生的错误重定向到特定站点? (例如:Error.aspx)

4 个答案:

答案 0 :(得分:1)

是的,您可以使用捕获任何未处理错误的自定义错误页面。您可以在该页面上编写代码,向您发送包含异常详细信息的错误。

Custom Error page

答案 1 :(得分:1)

在你的Global.asax中创建一个Application_Error方法并在那里处理错误。您可以使用Server.GetLastError()

获取最后一个异常

答案 2 :(得分:1)

我相信更好的方法是编写自己的异常模块和处理程序。这种方式的东西。

public sealed class ErrorCapture : IHttpModule
{

    #region IHttpModule Members

    public void Dispose()
    {
        //nothing here
    }

    public void Init(HttpApplication context)
    {
        context.Error += new EventHandler(context_Error);
    }

    void context_Error(object sender, EventArgs e)
    {
        // Do your implementation like send mail
        // Get Required details and use response.write for 
        // a good error msg display
        HttpApplication Application = (HttpApplication)sender;
        HttpServerUtility Server = Application.Server;
        HttpResponse Response = Application.Response;

        Exception error = Server.GetLastError();
        if (error.GetType().Equals(typeof(HttpUnhandledException)))
            error = error.InnerException;

        //your stuff

        Server.ClearError();//Do this
    }

    #endregion
}

答案 3 :(得分:0)

使用customErrors重定向到错误页面。

对于发送带有自定义类的邮件,我建议您编写一个自定义的asp.net健康监控提供程序。 看看这篇文章:http://www.tomot.de/en-us/article/6/asp.net/how-to-create-a-custom-healthmonitoring-provider-that-sends-e-mails 它向您展示了如何做到这一点。