Vadym
Vadym

Reputation: 86

Xamarin Forms Application Crashes without log

I'm developing some application using Xamarin Forms that has a function of route following. When it is used in real conditions on a vehicle it crashes whithout any logs in spite of the fact that I'm using AppCenter, i.e. in App.xaml.cs OnStart I added

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

In fact I tested my app in AppCenter by using Crashes.GenerateTestCrash() and got a test crash report. Besides I caught some errors using this approach. But now there is at least one reason that makes my app crash without any messages to AppCenter. Are there any clues how to resolve this problem?

Upvotes: 5

Views: 1473

Answers (1)

Nkosi
Nkosi

Reputation: 247471

Try using an actual asynchronous event handler.

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

OnStart is a simple void method and not an actual event handler. This mean that when made async it will become a fire and forget function which wont allow you to catch any exceptions that may have occurred within it.

If it is not caught at startup then it probably means that you are doing something similar somewhere within the app using a non event async void that goes uncaught and is crashing the application.

Start by doing a search for any async void in your code and checking to make sure that it is an actual event handler.

Upvotes: 2

Related Questions