firebase / quickstart-unity

Firebase Quickstart Samples for Unity
https://firebase.google.com/games
Apache License 2.0
820 stars 424 forks source link

[Question] Is there any way to enable Crashlytics in the Editor? #1164

Open dginovker opened 2 years ago

dginovker commented 2 years ago

[REQUIRED] Please fill in the following fields:

[REQUIRED] Please describe the question here:

When using Crashlytics, I'm able to use lines like

Crashlytics.LogException(new Exception("Test logging exception with LogException!"));

or

throw new Exception("Test exception by throwing it!");

And they appear in Firebase just fine, but only on builds and not in the editor. For testing purposes, I'd like to enable them in the editor too, then just filter them out on Firebase's end like with Unity crashlytics.

Is this possible?

dginovker commented 2 years ago

Doing some poking around in the code, it doesn't look like this sort of thing is supported, so I've made a wrapper function that at least logs it instead:

public static class FirebaseLog
{
    private static string AppendStacktrace(string msg)
    {
        // Needed because LogException doesn't include stacktrace
        // Replacing newlines with 5 spaces since crashlytics doesn't display newlines
        return $"{msg}     {Environment.StackTrace.Replace("\n", "     ")}";
    }

    public static void FirebaseLogError(string msg)
    {
        Crashlytics.SetCustomKey("Log Severity", "Error");
        Crashlytics.LogException(new Exception(AppendStacktrace(msg)));
        Debug.LogError(msg);
        Crashlytics.SetCustomKey("Log Severity", "");
    }

    public static void FirebaseLogWarn(string msg)
    {
        Crashlytics.SetCustomKey("Log Severity", "Warn");
        Crashlytics.LogException(new Exception(AppendStacktrace(msg)));
        Debug.LogWarning(msg);
        Crashlytics.SetCustomKey("Log Severity", "");
    }

    public static void FirebaseLogDebug(string msg)
    {
        Crashlytics.SetCustomKey("Log Severity", "Debug");
        Crashlytics.LogException(new Exception(AppendStacktrace(msg)));
        Debug.Log(msg);
        Crashlytics.SetCustomKey("Log Severity", "");
    }
}

Hopefully someone finds this useful ~~

paulinon commented 2 years ago

Hi @dginovker,

The reason this isn't supported is because the crash reports are sent only once the app has been relaunched, so it's important that you build your app for this.

With that being said, would you like this feature to be supported in future releases?

dginovker commented 2 years ago

Hi @paulinon ~

Yes, I would love to see a feature like that supported. Thanks for the clarification as well.

SabreRunner commented 2 months ago

I expect that all crashlytics logs, regardless of device, to be sent immediately (or after X logs or Y seconds) and then on restart, it'll check what is saved vs what was sent and fill in the blanks.