Skip to content

Commit

Permalink
Log unhandled exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
OvrBtn committed Mar 3, 2025
1 parent 3230933 commit 0613c82
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
16 changes: 15 additions & 1 deletion StudentUsos/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ public partial class App : Application

public static IServiceProvider ServiceProvider { get; private set; }
FirebasePushNotificationsService firebasePushNotificationsService;
ILogger logger;
public App(IServiceProvider serviceProvider,
FirebasePushNotificationsService firebasePushNotificationsService)
FirebasePushNotificationsService firebasePushNotificationsService,
ILogger logger = null)

Check warning on line 17 in StudentUsos/App.xaml.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
ServiceProvider = serviceProvider;
this.firebasePushNotificationsService = firebasePushNotificationsService;
this.logger = logger;

InitializeComponent();

Expand All @@ -24,6 +27,17 @@ public App(IServiceProvider serviceProvider,
SetLanguageFromLocalStorage();

CalendarSettings.LoadNotificationSettingsAndInitializePreferences();

AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
logger.Log(LogLevel.Fatal, e.ExceptionObject.ToString()!);
};

TaskScheduler.UnobservedTaskException += (sender, e) =>
{
logger.Log(LogLevel.Fatal, e.Exception.ToString());
e.SetObserved();
};
}

//Called after Android MainActivity ctor
Expand Down
11 changes: 11 additions & 0 deletions StudentUsos/Platforms/Android/MainApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ public MainApplication(IntPtr handle, JniHandleOwnership ownership)
{
}

public override void OnCreate()
{
base.OnCreate();

AndroidEnvironment.UnhandledExceptionRaiser += (sender, e) =>
{
Logger.Default?.Log(LogLevel.Fatal, e.Exception.ToString());
e.Handled = true;
};
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
8 changes: 7 additions & 1 deletion StudentUsos/Services/Logger/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ async Task RunLogCleanupAsync()
const int deleteLogsAfterDays = 1;
var deleteThresholdUnixtime = DateTimeOffset.UtcNow.AddDays(deleteLogsAfterDays * -1).ToUnixTimeSeconds();
localDatabaseManager.Value.Remove<LogRecord>($"CreationDateUnix < {deleteThresholdUnixtime} AND IsSynchronizedWithServer = 1");

//if unhandled exception happens it might not get sent to server, this makes sure it does
if (localDatabaseManager.Value.Get<LogRecord>(x => x.LogLevel == LogLevel.Fatal.ToString()) is not null)
{
await TrySendingLogsToServerAsync();
}
}

List<string>? allowedModules = null;
Expand Down Expand Up @@ -173,7 +179,7 @@ async Task TrySendingLogsToServerAsync()
var result = await serverConnectionManager.Value.SendAuthorizedPostRequestAsync("logs/log", serialized, AuthorizationMode.Full);
if (result is not null && result.IsSuccess)
{
localDatabaseManager.Value.ExecuteQuery($"UPDATE {nameof(LogRecord)} SET {nameof(LogRecord.IsSynchronizedWithServer)} = 1 WHERE CreationDateUnix <= {unixTime};");
localDatabaseManager.Value.ExecuteQuery($"UPDATE {nameof(LogRecord)} SET {nameof(LogRecord.IsSynchronizedWithServer)} = 1 WHERE {nameof(LogRecord.CreationDateUnix)} <= {unixTime};");
}
}
}
Expand Down

0 comments on commit 0613c82

Please sign in to comment.