每个appdomain的webapi和未处理的exception挂钩

拥有WebApi 2.2应用程序 – 是否可以使用AppDomain的UnhandledException钩子?

我有一个类似于这样的代码:

[assembly: OwinStartup("DevConfiguration", typeof(DevStartup))] namespace WebApi.Module { public class DevStartup { private const string ErrorEventSourceName = "WebApiHost"; private const int ErrorEventId = 600; [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)] public void Configuration(IAppBuilder app) { AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException; ... throw new Exception("some exception); } public static void AppDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e) { EventLog.WriteEntry(ErrorEventSourceName, ex.ToString(),EventLogEntryType.Error, ErrorEventId); } } } 

UnhandeledException挂钩永远不会被调用…原因是什么 – 如何使这个工作并捕获所有未捕获的exception(在全局exception处理程序旁边 – 它将处理请求上下文中的exception)。