You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.6 KiB
101 lines
3.6 KiB
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using MultiTerm.Core.ViewModel;
|
|
using System.Windows;
|
|
using MultiTerm.Core.Helpers;
|
|
using Common.Logging;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Common.AppSettings;
|
|
using MultiTerm.Protocols.Helpers;
|
|
|
|
namespace MultiTerm.Wpf;
|
|
|
|
public partial class App : Application
|
|
{
|
|
private static ILogger? logger;
|
|
|
|
public static IHost? AppHost { get; private set; }
|
|
|
|
public App()
|
|
{
|
|
// create dependency injection host
|
|
AppHost = Host.CreateDefaultBuilder()
|
|
.ConfigureServices((hostContext, services) =>
|
|
{
|
|
services.AddSingleton<MainWindow>();
|
|
services.AddSingleton<ILogger>(new SerilogLogger("C:/log/multiterm-log-.txt", true));
|
|
services.AddSingleton<IAppSettingsProvider>(new XmlAppSettingsProvider("C:/log/multiterm-config.xml"));
|
|
|
|
// viewmodels
|
|
services.AddSingleton<ShellViewModel>();
|
|
|
|
// application specific
|
|
services.AddCommunicationProtocolFactory();
|
|
services.AddTerminalViewModelFactory();
|
|
})
|
|
.Build();
|
|
}
|
|
|
|
protected override async void OnStartup(StartupEventArgs e)
|
|
{
|
|
await AppHost!.StartAsync();
|
|
|
|
// create logger and initialize
|
|
logger = AppHost.Services.GetRequiredService<ILogger>();
|
|
logger.Initialize(LogLevel.Trace);
|
|
logger.LogInfo("Application started.", nameof(App));
|
|
|
|
// register handlers to some hidden exception types
|
|
AppDomain.CurrentDomain.UnhandledException += this.AppDomain_UnhandledException;
|
|
Application.Current.DispatcherUnhandledException += this.Application_DispatcherUnhandledException;
|
|
TaskScheduler.UnobservedTaskException += this.TaskScheduler_UnobservedTaskException;
|
|
|
|
// register log events from AppSettingsProvider and load saved settings
|
|
var appSettingsProvider = AppHost.Services.GetRequiredService<IAppSettingsProvider>();
|
|
appSettingsProvider.LogWorthyEvent += AppSettingsProvider_LogWorthyEvent;
|
|
appSettingsProvider.Load();
|
|
|
|
// instanciate startup form and show
|
|
var startupForm = AppHost.Services.GetRequiredService<MainWindow>();
|
|
startupForm.Show();
|
|
|
|
base.OnStartup(e);
|
|
}
|
|
|
|
private void AppSettingsProvider_LogWorthyEvent(object? sender, LogEntry e)
|
|
{
|
|
logger?.Log(e);
|
|
}
|
|
|
|
protected override async void OnExit(ExitEventArgs e)
|
|
{
|
|
// save settings to persistent location
|
|
var appSettingsProvider = AppHost!.Services.GetRequiredService<IAppSettingsProvider>();
|
|
appSettingsProvider.Save();
|
|
|
|
// log application exit and stop logger (if still available)
|
|
logger?.LogInfo("Application exited.", nameof(App));
|
|
logger?.StopLogging();
|
|
|
|
await AppHost!.StopAsync();
|
|
base.OnExit(e);
|
|
}
|
|
|
|
#region Event handlers for hidden exceptions
|
|
private void AppDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
{
|
|
logger?.LogException((Exception)e.ExceptionObject, "AppDomain UnhandledException caught", nameof(App));
|
|
}
|
|
|
|
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
|
|
{
|
|
logger?.LogException(e.Exception, "Application DispatcherUnhandledException caught", nameof(App));
|
|
}
|
|
|
|
private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
|
|
{
|
|
logger?.LogException(e.Exception, "TaskScheduler UnobservedTaskException caught", nameof(App));
|
|
}
|
|
#endregion
|
|
}
|
|
|