Add unhandled error handling and crash logging to chardonnay

This commit is contained in:
Mbucari 2023-07-07 14:14:12 -06:00
parent 9243aa47e7
commit 4df9e5abbf

View File

@ -1,7 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ApplicationServices;
using AppScaffolding;
@ -35,6 +34,7 @@ namespace LibationAvalonia
$"\"{Configuration.ProcessDirectory}\"");
return;
}
AppDomain.CurrentDomain.UnhandledException += (o, e) => LogError(e.ExceptionObject);
//***********************************************//
// //
@ -42,6 +42,8 @@ namespace LibationAvalonia
// //
//***********************************************//
// Migrations which must occur before configuration is loaded for the first time. Usually ones which alter the Configuration
try
{
var config = LibationScaffolding.RunPreConfigMigrations();
//Start as much work in parallel as possible.
@ -50,9 +52,11 @@ namespace LibationAvalonia
if (config.LibationSettingsAreValid)
{
if (!RunDbMigrations(config))
return;
// most migrations go in here
LibationScaffolding.RunPostConfigMigrations(config);
LibationScaffolding.RunPostMigrationScaffolding(Variety.Chardonnay, config);
//Start loading the library before loading the main form
App.LibraryTask = Task.Run(() => DbContexts.GetLibrary_Flat_NoTracking(includeParents: true));
}
@ -60,6 +64,11 @@ namespace LibationAvalonia
classicLifetimeTask.Result.Start(null);
}
catch(Exception e)
{
LogError(e);
}
}
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
@ -67,20 +76,35 @@ namespace LibationAvalonia
.LogToTrace()
.UseReactiveUI();
public static bool RunDbMigrations(Configuration config)
private static void LogError(object exceptionObject)
{
var logError = $"""
{DateTime.Now} - Libation Crash
OS {Configuration.OS}
Version {LibationScaffolding.BuildVersion}
ReleaseIdentifier {LibationScaffolding.ReleaseIdentifier}
InteropFunctionsType {InteropFactory.InteropFunctionsType}
LibationFiles {getConfigValue(c => c.LibationFiles)}
Books Folder {getConfigValue(c => c.Books)}
=== EXCEPTION ===
{exceptionObject}
""";
var crashLog = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "LibationCrash.log");
using var sw = new StreamWriter(crashLog, true);
sw.WriteLine(logError);
static string getConfigValue(Func<Configuration, string> selector)
{
try
{
// most migrations go in here
LibationScaffolding.RunPostConfigMigrations(config);
LibationScaffolding.RunPostMigrationScaffolding(Variety.Chardonnay, config);
return true;
return selector(Configuration.Instance);
}
catch (Exception exDebug)
catch (Exception ex)
{
Serilog.Log.Logger.Debug(exDebug, "Silent failure");
return false;
return ex.ToString();
}
}
}
}