Xamarin Forms 应用程序崩溃没有日志

Vad*_*dym 5 forms crash report xamarin

我正在使用具有路线跟踪功能的 Xamarin Forms 开发一些应用程序。尽管我正在使用 AppCenter,即在 App.xaml.cs OnStart 我添加

    protected async override void OnStart()
    {
        AppCenter.Start("android=__mycode___" +
                          "uwp={Your UWP App secret here};" +
                          "ios={Your iOS App secret here}",
                          typeof(Analytics), typeof(Crashes));


        bool hadMemoryWarning = await Crashes.HasReceivedMemoryWarningInLastSessionAsync();
        ErrorReport crashReport = await Crashes.GetLastSessionCrashReportAsync();

        if (crashReport != null)
        {
            Analytics.TrackEvent(crashReport.StackTrace);
        }
        
    }
Run Code Online (Sandbox Code Playgroud)

事实上,我在 AppCenter 中使用 Crashes.GenerateTestCrash() 测试了我的应用程序并获得了测试崩溃报告。此外,我使用这种方法发现了一些错误。但是现在至少有一个原因使我的应用程序崩溃而没有向 AppCenter 发送任何消息。有什么线索可以解决这个问题吗?

Nko*_*osi 2

尝试使用实际的异步事件处理程序。

private event EventHandler onStart = delegate { };

protected override void OnStart() {
    onStart += handleStart; //subscribe
    onStart(this, EventArgs.Empty); //raise event
}

private async void handleStart(object sender,EventArgs args) {
    onStart -= handleStart; //unsubscribe
    try {
        AppCenter.Start("android=__mycode___" +
                          "uwp={Your UWP App secret here};" +
                          "ios={Your iOS App secret here}",
                          typeof(Analytics), typeof(Crashes));


        bool hadMemoryWarning = await Crashes.HasReceivedMemoryWarningInLastSessionAsync();
        ErrorReport crashReport = await Crashes.GetLastSessionCrashReportAsync();

        if (crashReport != null) {
            Analytics.TrackEvent(crashReport.StackTrace);
        }
    } catch (Exception ex) {
        //...handle exception or log as needed
    }        
}
Run Code Online (Sandbox Code Playgroud)

OnStart是一个简单的 void 方法,而不是实际的事件处理程序。这意味着当异步时,它将成为一个即发即忘的函数,不允许您捕获其中可能发生的任何异常。

如果它在启动时没有被捕获,那么这可能意味着您正在应用程序中的某个位置使用未async void捕获的非事件执行类似的操作并使应用程序崩溃。

首先搜索async void代码中的任何内容并检查以确保它是实际的事件处理程序。