Ilia Stoilov
Ilia Stoilov

Reputation: 632

Xamarin Forms save exceptions in a file

I want to be able to view and save in a file the exceptions thrown by my app when it crashes. Is there a tutorial on this topic? I've seen the Advanced App Lifecycle Demos and the crash handling there, but I don't really understand how to implement it in my app.

Upvotes: 1

Views: 334

Answers (3)

Jauhenka
Jauhenka

Reputation: 162

In our team we didn't enjoyed the Xamarin Insights so we use such a construction:

    public async Task SafeCall(Func<Task> action)
    {
        var workedWrong = false;
        try
        {
            await action();
        }
        catch (Exception e)
        {
            workedWrong = true;
            // Here you copy all the data you need.
        }

        if (workedWrong)
        {
            // Here you show the notification that something went wrong and write all the relevant information to the file (you can find how to do it in official Xamarin manual).
        }
    }

Although it doesn't catch the iOS exceptions.

Upvotes: 0

Wosi
Wosi

Reputation: 45243

Getting the exception that caused an entire crash is not easy to archieve on your own. Luckily there's a way to get the exception using Xamarin Insights. It's free to use for Xamarin customers.

You simply need to get an API key for your app on the website, add the NuGet package Xamarin.Isights and initialize it as described here in your app. Then you will receive crash reports everytime the user starts your app after a crash.

If you encounter startup crashes then you can use these lines of code to initialize Xamarin.Insights to receive reports for them:

Insights.HasPendingCrashReport += (sender, isStartupCrash) =>
{
  if (isStartupCrash) {
    Insights.PurgePendingCrashReports().Wait();
  }
};
Insights.Initialize("Your API Key");

Upvotes: 3

Jason
Jason

Reputation: 89102

You'd be better off using Xamarin Insights, an application logging framework that will automatically log crashes and unhandled exceptions for you.

Upvotes: 2

Related Questions