本文介绍了global.asax 中的 Application_Error 未捕获 WebAPI 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在从事的项目,我们正在实施的其中一项内容是我们在我的一些团队较旧的 ASP.NET 和 MVC 项目中拥有代码的内容 - Application_Error 异常捕获器向开发团队发送一封电子邮件,其中包含异常体验和最相关的详细信息.

For a project I am working on, one of the things we're implementing is something that we have code for in some of my teams older ASP.NET and MVC projects - an Application_Error exception catcher that dispatches an email to the development team with the exception experience and most relevant details.

外观如下:

Global.asax:

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    string path = "N/A";
    if (sender is HttpApplication)
        path = ((HttpApplication) sender).Request.Url.PathAndQuery;

    string args = string.Format("<b>Path:</b> {0}", path);

    // Custom code that generates an HTML-formatted exception dump
    string message = Email.GenerateExceptionMessage(ex, args);

    // Custom code that sends an email to the dev team.
    Email.SendUnexpectedErrorMessage("Some App", message);
}

不过,有一个小"问题——当我故意让一部分代码抛出异常以测试这种机制时...

One "minor" problem, though - when I intentionally have a part of the code throw an exception in order to test this mechanism...

public static void GetMuffinsByTopping(string topping)
{
    throw new Exception("Test Exception!", new Exception("Test Inner Exception!!!"));

    // Actual repository code is unreachable while this test code is there
}

前端 JavaScript 立即拦截了一个 HTTP 500 请求,但是没有到达上面提到的 global.asax.cs 代码(我在方法的第一行执行了一个断点.)

The front-end JavaScript is immediately intercepting an HTTP 500 request, but the global.asax.cs code noted above is not being reached (I set a breakpoint on the first executing line of the method.)

问题:我可以通过什么方式让旧的"Application_Error 处理程序发送错误电子邮件,以便我们团队的开发人员可以更轻松地调试我们的应用程序?>

Question: In what way can I get the "old" Application_Error handler to dispatch error emails, so that our team's developers can more easily debug our application?

推荐答案

将您的错误处理逻辑从 Application_Error 抽象到它自己的函数中.创建一个 Web API 异常过滤器.

Abstract out your error handling logic from Application_Error into its own function. Create a Web API exception filter.

//register your filter with Web API pipeline
//this belongs in the Application_Start event in Global Application Handler class (global.asax)
//or some other location that runs on startup
GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());

//Create filter
public class LogExceptionFilterAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        ErrorLogService.LogError(context.Exception);
    }
}

//in global.asax or global.asax.cs
protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    ErrorLogService.LogError(ex);
}

//common service to be used for logging errors
public static class ErrorLogService
{
    public static void LogError(Exception ex)
    {
        //Email developers, call fire department, log to database etc.
    }
}

来自 Web API 的错误不会触发 Application_Error 事件.但是我们可以创建一个异常过滤器并注册它来处理错误.另请参阅 ASP.NET 中的全局错误处理网络 API 2.

Errors from Web API do not trigger the Application_Error event. But we can create an exception filter and register it to handle the errors. Also see Global Error Handling in ASP.NET Web API 2.

这篇关于global.asax 中的 Application_Error 未捕获 WebAPI 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:24