Application_Error未在Global.asax.cs中触发

时间:2018-04-11 00:45:47

标签: c# asp.net-mvc visual-studio-2017

我想要做的只是在我的应用程序中记录任何未处理的异常:

的Global.asax.cs

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {

        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

    protected void Application_Error(Object sender, EventArgs e)
    {
        var logger = new Logger();
        var ex = Server.GetLastError();
        logger.LogError(ex, "global");
    }
}

的web.config

<customErrors mode="On" defaultRedirect="~/ErrorHandler/Index" />

当我打电话

throw new Exception("testy");
HomeController中的

Application_Error无法触发。

这种方法有什么意义?我有这个困惑吗?

0 个答案:

没有答案
相关问题