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

标签 c# wpf crash-reports hockeyapp

我启动了一个空白的 WPF 应用程序(如下所示)来实现 HockeyApp 崩溃报告。当程序启动时,会弹出一个窗口,其中包含一个按钮供用户按下。当用户单击它时,处理程序会尝试除以零并使应用程序崩溃。我收到了崩溃报告,一切都运行顺利,直到我模仿了我们更大系统的错误捕获方法,即使用 DispatcherUnhandledException捕获“未捕获”异常的事件处理程序,然后调用 System.Environment.Exit(0)在后台优雅地结束任何事情。现在 HockeyApp api 不发送崩溃报告。我想知道在更高级别捕获异常是否会让 HockeyApp 认为“哦,他们控制住了一切”并且不会记录“崩溃”。

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

应用类:

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);
    }
}

主窗口类:

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 是最后的步骤,只有在您没有希望恢复某些有效状态时才应使用。

关于c# - System.Environment.Exit(0) 防止 hockeyapp 注册崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45214663/

相关文章:

c# - IIS 8.5 连接超时总是_60 秒

c# - 如何在 WPF 中从 C# 更改字体颜色

css - 如何理解这个崩溃报告? SIGSEGV

android - 使用 Kivy Launcher 运行时无法找到 kivy 日志

c# - Sonarqube 规则的限制

c# - 如何使用 dotnet 或 powershell 检测特定进程是否被提升

c# - 当我单击 ListBoxItem 时,如何使 StackPanel 滑到一边

wpf - 计算 WPF 中的选择画笔颜色

ios - 未知的崩溃原因(附有 CrashReport)SIGTRAP。 AF网络?

c# - 使用电子邮件地址的 Asp.Net 身份验证