From 5ad927dc299fb8aef976ebb0f480775673090c45 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Sat, 8 Apr 2023 15:42:46 +0200 Subject: [PATCH] added log entries for hidden exceptions, added log entry for when a new terminal is opened --- MultiTerm.Core/ViewModel/ShellViewModel.cs | 8 +++++-- MultiTerm.Wpf/App.xaml.cs | 28 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/MultiTerm.Core/ViewModel/ShellViewModel.cs b/MultiTerm.Core/ViewModel/ShellViewModel.cs index a564e49..e0373a5 100644 --- a/MultiTerm.Core/ViewModel/ShellViewModel.cs +++ b/MultiTerm.Core/ViewModel/ShellViewModel.cs @@ -1,4 +1,5 @@ -using Common.StartupHelpers; +using Common.Logging; +using Common.StartupHelpers; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using MultiTerm.Core.Common; @@ -35,10 +36,12 @@ public partial class ShellViewModel : ObservableObject #endregion private readonly ITerminalViewModelFactory terminalViewModelFactory; + private readonly ILogger logger; - public ShellViewModel(ITerminalViewModelFactory terminalViewModelFactory) + public ShellViewModel(ITerminalViewModelFactory terminalViewModelFactory, ILogger logger) { this.terminalViewModelFactory = terminalViewModelFactory; + this.logger = logger; // TEMP Init this.AppendTerminalWithSelectedViewType(ProtocolType.Serial); } @@ -46,6 +49,7 @@ public partial class ShellViewModel : ObservableObject [RelayCommand] private void AppendTerminalWithSelectedViewType(ProtocolType protocolType) { + this.logger.LogInfo($"Adding new Terminal with ViewType '{this.SelectedTerminalViewType}' and ProtocolType '{protocolType}'", nameof(ShellViewModel)); this.AppendConfiguredTerminal(this.terminalViewModelFactory.Create(this.SelectedTerminalViewType, protocolType)); } diff --git a/MultiTerm.Wpf/App.xaml.cs b/MultiTerm.Wpf/App.xaml.cs index b02790e..dcb58aa 100644 --- a/MultiTerm.Wpf/App.xaml.cs +++ b/MultiTerm.Wpf/App.xaml.cs @@ -5,11 +5,15 @@ using System.Windows; using MultiTerm.Core.Helpers; using Common.Logging; using Common.Logger; +using System; +using System.Threading.Tasks; namespace MultiTerm.Wpf; public partial class App : Application { + private static ILogger? logger; + public static IHost? AppHost { get; private set; } public App() @@ -35,10 +39,15 @@ public partial class App : Application await AppHost!.StartAsync(); // create logger and initialize - var logger = AppHost.Services.GetRequiredService(); + logger = AppHost.Services.GetRequiredService(); 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; + // instanciate startup form and show var startupForm = AppHost.Services.GetRequiredService(); startupForm.Show(); @@ -49,7 +58,6 @@ public partial class App : Application protected override async void OnExit(ExitEventArgs e) { // log application exit and stop logger (if still available) - var logger = AppHost!.Services.GetRequiredService(); logger?.LogInfo("Application exited.", nameof(App)); logger?.StopLogging(); @@ -57,4 +65,20 @@ public partial class App : Application 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 }