Forum Xamarin Xamarin.Android

Log crashes when app is closed while receiving BG notifications

dai15002dai15002 Member ✭✭✭

In my app, I have implemented BG notifications that run even when the app in the background. However when the app is dead the BG notification is fired but something crashes the app (only when app is closed/dead). I have implemented a log file which shows the last crash on phone but it doesn't log the crashes when the app is dead. Below is my implmenetation:

 [Conditional("DEBUG")]
    internal static void LogUnhandledException(Exception exception)
    {
        try
        {
            const string errorFileName = "Fatal.log";
            var libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // iOS: Environment.SpecialFolder.Resources
            var errorFilePath = Path.Combine(libraryPath, errorFileName);
            var errorMessage = String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}",
            DateTime.Now, exception.ToString());
            File.WriteAllText(errorFilePath, errorMessage);

            // Log to Android Device Logging.
            Android.Util.Log.Error("Crash Report", errorMessage);

        }
        catch
        {
            // just suppress any error logging exceptions
        }
    }

    /// <summary>
    // If there is an unhandled exception, the exception information is diplayed 
    // on screen the next time the app is started (only in debug configuration)
    /// </summary>
    [Conditional("DEBUG")]
    private void DisplayCrashReport()
    {
        const string errorFilename = "Fatal.log";
        var libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var errorFilePath = Path.Combine(libraryPath, errorFilename);

        if (!File.Exists(errorFilePath))
        {
            return;
        }

        var errorText = File.ReadAllText(errorFilePath);
        new AlertDialog.Builder(this)
            .SetPositiveButton("Clear", (sender, args) =>
            {
                File.Delete(errorFilePath);
            })
            .SetNegativeButton("Close", (sender, args) =>
            {
                // User pressed Close.
            })
            .SetMessage(errorText)
            .SetTitle("Crash Report")
            .Show();
    }

How can I log crashes even when the app is dead?

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    but it doesn't log the crashes when the app is dead.

    When your application is closed none of its code will be run. So this collection method makes no sense to a closed application. It will only record or display error messages when you manually call it at active time.
    I'm curious about one point that your application has already been at the closed state how did it crash again? Your application will only resume after the user taps the notification to open it.

  • dai15002dai15002 Member ✭✭✭

    What is wierd is that the BG notifications sent to Samsung devices every half hours are received but somewhere inside the execution code it crashes. These executions happen every half hours even when the application is closed from running in BG, because the Samsung OS notifys that there has been a crash inside the app every half hours (exactly when the notifications are received), which means the app is running the notifications but crashes somewhere.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Have you tried other devices?
    The system handles the notification when your application is closed and none of your code will be executed at that state. How did the Samsung send a crashing report to you?

  • dai15002dai15002 Member ✭✭✭

    Yes I have tried with different devices and this is something that occurs only on Samsung.
    Samsung is showing a notification saying my app is draining the battery, and when tapping on the notif a list of app krasches are shown that are happend when the app's BG notification is received.

  • LandLuLandLu Member, Xamarin Team Xamurai

    If it only occurs on some particular devices this could be a hardware issue.
    Normally, a closed app can't be crashed again as all the process of the application has been stopped.

  • dai15002dai15002 Member ✭✭✭

    Maybe it is so, since I only experience this on the newer Samsung devices and not other android phones

  • LandLuLandLu Member, Xamarin Team Xamurai

    If it only occurs on some special devices I'm afraid it is not caused by your code or Xamarin. It's hard to locate the issue as I don't have a Samsung phone aside.
    I suggest opening a support ticket here:
    https://support.microsoft.com/en-us/supportforbusiness/productselection?sapId=211dd84f-3474-c3c5-79bf-66db630c92a6

Sign In or Register to comment.