System.Environment.Exit(0) 防止 hockeyapp 注册崩溃

System.Environment.Exit(0) preventing hockeyapp from registering a crash

我启动了一个空白的 WPF 应用程序(如下所示)来实现 HockeyApp 崩溃报告。当程序启动时,window 弹出一个按钮供用户按下。当用户单击它时,处理程序会尝试除以零并使应用程序崩溃。我收到了崩溃报告,一切都 运行 顺利,直到我模仿了我们更大系统的错误捕获方法,即使用 DispatcherUnhandledException Event Handler to catch "uncaught" exceptions and then call System.Environment.Exit(0) 在后台优雅地结束任何事情。现在 HockeyApp api 没有发送崩溃报告。我想知道是否在更高级别捕获异常会使 HockeyApp 认为 "Oh, they got things under control" 并且不会注册 "crash."

我目前正在与 HockeyApp 支持人员讨论这个问题,但我想知道是否还有其他人遇到过这个问题。我应该删除 Exit(0) 行,还是在我们有未捕获的异常时退出应用程序有更好的做法吗?我尝试将错误代码从 0 更改为 574 (ERROR_UNHANDLED_EXCEPTION),但没有结果。我认为除了 HockeyApp api 已有的数据外,我们不需要保存任何数据。

应用class:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        RegisterHockeyAppCrashReporting();

        Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
    }

    private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        System.Environment.Exit(0);
    }

    private async void RegisterHockeyAppCrashReporting()
    {
        HockeyClient.Current.Configure(AppConstants.APP_ID)
            .SetContactInfo(AppConstants.USER_NAME, AppConstants.USER_EMAIL);
        await HockeyClient.Current.SendCrashesAsync(true);
    }
}

主窗口class:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var zero = 0;
        var number = 1;
        var crash = number / zero;
    }
}

Environement.Exit 立即 终止您的应用程序。所以,我想,Hockey 什么都不做这一事实并不奇怪。

Exit terminates an application immediately, even if other threads are running. If the return statement is called in the application entry point, it causes an application to terminate only after all foreground threads have terminated.

If Exit is called from a try or catch block, the code in any finally block does not execute. If the return statement is used, the code in the finally block does execute.

最佳实践往往是基于意见和情况的。例如,我们记录未处理异常的堆栈跟踪,然后在我们的 UWP 应用程序中调用 Environement.FailFast(尽管我们不使用 Hockey 应用程序)。我们的逻辑很简单——我们的记录器设施可能还活着,但我们对应用程序的其余部分不太确定。如果连记录器设施都不能正常工作,我们也无能为力。恕我直言 ExitFailFast 是最后的步骤,只有在您没有希望恢复某些有效状态时才应使用。