/// <summary> /// Unhandled exception handler. If an unhandled exception crashes the program, save /// the stack trace to a log file. /// </summary> /// <param name="sender">The AppDomain.</param> /// <param name="e">The details of the unhandled exception.</param> private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Exception ex = (Exception)e.ExceptionObject; Logger.LogCritical(ex, "Unhandled exception"); WarningDialog.Show("The program failed to handle an exception.", "Unhandled exception"); }
/// <summary> /// Function that's run when the program first starts. /// Set up the data context links with the local variables. /// </summary> public MainWindow( ViewModels.ViewModel model, Navigation.AvaloniaNavigationService navigationService, ILogger <MainWindow> logger) { // Initialize the readonly properties. this.ViewModel = model; this.NavigationService = navigationService; this.Logger = logger; try { // Set up an event handler for any otherwise unhandled exceptions in the code. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; // Initialize the window. AvaloniaXamlLoader.Load(this); // Set the title. Title = $"{SystemInfo.ProductInfo.Name} - {SystemInfo.ProductInfo.Version}"; // Load configuration data Collections.QuestCollection?quests = null; string?currentQuest = null; try { this.Logger.LogDebug("Loading configuration."); Config.NetTallyConfig.Load(out quests, out currentQuest, Options.AdvancedOptions.Instance); this.Logger.LogInformation("Configuration loaded."); } catch (ConfigurationErrorsException e) { this.Logger.LogError(e, "Failure during configuration."); WarningDialog.Show("Error in configuration. Current configuration ignored.", "Error in configuration"); } // Complete the platform setup. this.PlatformSetup(quests, currentQuest); } catch (Exception e) { this.Logger.LogError(e, "Failure during program startup."); WarningDialog.Show("Unable to start the program.", "Failure on startup"); this.Close(); } }
/// <summary> /// Saves the configuration. /// </summary> private void SaveConfig() { try { string selectedQuest = ViewModel.SelectedQuest?.ThreadName ?? ""; Config.NetTallyConfig.Save(ViewModel.QuestList, selectedQuest, Options.AdvancedOptions.Instance); Logger.LogDebug("Configuration saved."); } catch (Exception ex) { Logger.LogWarning(ex, "Failed to save configuration."); WarningDialog.Show("The program failed to save configuration data.", "Failed to save configuration"); } }
/// <summary> /// Handles the ExceptionRaised event of the MainViewModel control. /// This is called anytime there's an exception generated that needs /// to propogate up to the UI. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param> private void MainViewModel_ExceptionRaised(object?sender, CustomEventArgs.ExceptionEventArgs e) { Exception ex = e.Exception; string exmsg = ex.Message; var innerEx = ex.InnerException; while (innerEx != null) { exmsg = exmsg + "\n" + innerEx.Message; innerEx = innerEx.InnerException; } WarningDialog.Show(exmsg, "Error"); if (!(ex.Data.Contains("Application"))) { Logger.LogError(ex, "Exception bubbled up from the view model."); } e.Handled = true; }