Firebase Crashlytics is not logging error in Maui .net 8.0 ?
Debasis Patra
0
Reputation points
MainPage.xaml.cs
================
private void OnCounterClicked(object sender, EventArgs e)
{
_firebaseCrashlyticsService.Log(new Exception ("test"));
try
{
count++;
if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";
SemanticScreenReader.Announce(CounterBtn.Text);
throw new Exception("Testing Error");
}
catch (Exception ex)
{
_firebaseCrashlyticsService.Log(ex);
}
}
===============
MauiProgram.cs
================
private static MauiAppBuilder RegisterFirebase(this MauiAppBuilder builder)
{
builder.ConfigureLifecycleEvents(events =>
{
#if ANDROID
events.AddAndroid(android => android.OnCreate((activity, bundle) => {
Firebase.FirebaseApp.InitializeApp(activity);
}));
#endif
});
return builder;
}
===============================
IFirebaseCrashlyticsService.cs
===============================
#if ANDROID
using Firebase;
using Firebase.Crashlytics;
#endif
namespace PocForVersion;
public interface IFirebaseCrashlyticsService
{
void Log(Exception ex);
}
public class FirebaseCrashlyticsService : IFirebaseCrashlyticsService
{
public void Log(Exception ex)
{
#if ANDROID
var crashlytics = FirebaseCrashlytics.Instance;
if (crashlytics != null)
{
crashlytics.RecordException(Java.Lang.Throwable.FromException(ex));
}
else
{
// Handle the case where Crashlytics is not initialized
Console.WriteLine("FirebaseCrashlytics is not initialized.");
}
#endif
}
}
========================
google-services.json
=======================
{
"project_info": {
},
"client": [
{
"client_info": {
},
"oauth_client": [],
"api_key": [
{
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": []
}
}
}
],
"configuration_version": "1"
}
================
MainActivity.cs
==================
using Firebase;
using Firebase.Crashlytics;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Initialize Firebase
FirebaseApp.InitializeApp(this);
var firebase = FirebaseCrashlytics.Instance;
}
Sign in to answer