#nullable enable using System; using System.IO; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; using Microsoft.Extensions.DependencyInjection; using ReminderOrganizer.Services; using ReminderOrganizer.ViewModels; namespace ReminderOrganizer { public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); SetupExceptionHandling(); ServiceLocator.Initialize(); } private void SetupExceptionHandling() { AppDomain.CurrentDomain.UnhandledException += (s, e) => { LogException((Exception)e.ExceptionObject, "AppDomain.UnhandledException"); if (e.IsTerminating) Environment.Exit(1); }; DispatcherUnhandledException += (s, e) => { LogException(e.Exception, "DispatcherUnhandledException"); e.Handled = true; MessageBox.Show($"An unexpected error occurred:\n{e.Exception.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); }; TaskScheduler.UnobservedTaskException += (s, e) => { LogException(e.Exception, "UnobservedTaskException"); e.SetObserved(); }; } private static void LogException(Exception ex, string source) { try { var logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "error.log"); var logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{source}]\n{ex}\n\n"; File.AppendAllText(logPath, logEntry); } catch { } } } }