【问题标题】:AppDomain.CurrentDomain.UnhandledException not firing in MVCAppDomain.CurrentDomain.UnhandledException 未在 MVC 中触发
【发布时间】:2013-12-24 07:38:11
【问题描述】:

我有 MVC4 项目的默认模板,并在我的 Global.asax 中订阅了 UnhandledException 事件:

    [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
    protected void Application_Start()
    {
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    }

    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Debugger.Break();
        // Some logging code here
    }

在我的 HomeController 索引操作中,我简单地写:

    throw new Exception("Test exception"):

具有此类订阅的控制台应用程序可以正常工作。但在 MVC 项目处理程序调用中永远不会发生。 MVC 有什么问题?

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-5


【解决方案1】:

未调用 UnhandledException 回调,因为当动作引发异常时,它由 MVC 框架处理,并且 AppDomain 没有未处理的异常,因此它不是卸载的候选对象,它可以处理下一个请求。

记录异常的最短途径:

protected void Application_Error(object sender, EventArgs args)
{
    Debugger.Break();
    Exception error = ((HttpApplication)sender).Context.Server.GetLastError();
    // Some logging code here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-21
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    相关资源
    最近更新 更多