using System.IO; using System.Windows; using Explorer.Presentation.ViewModels; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; namespace Explorer.App; public partial class App : System.Windows.Application { private IHost? _host; protected override async void OnStartup(StartupEventArgs e) { base.OnStartup(e); DispatcherUnhandledException += (_, args) => { Log.Error(args.Exception, "Unhandled UI exception"); args.Handled = true; }; var env = new Windows.WindowsAppEnvironment(); Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() .WriteTo.File( Path.Combine(env.LogDirectory, "explorer-.log"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: 14) .CreateLogger(); _host = Host.CreateDefaultBuilder() .UseSerilog() .ConfigureServices((_, services) => services.AddExplorer()) .Build(); var vm = _host.Services.GetRequiredService(); await vm.InitializeAsync().ConfigureAwait(true); await _host.StartAsync().ConfigureAwait(true); var window = _host.Services.GetRequiredService(); window.DataContext = vm; window.Show(); } protected override async void OnExit(ExitEventArgs e) { if (_host is not null) { await _host.StopAsync(TimeSpan.FromSeconds(3)).ConfigureAwait(true); _host.Dispose(); } Log.CloseAndFlush(); base.OnExit(e); } }