added log entries for hidden exceptions,

added log entry for when a new terminal is opened
master
Jonas Arnold 3 years ago
parent 50dc48a3fc
commit 5ad927dc29
  1. 8
      MultiTerm.Core/ViewModel/ShellViewModel.cs
  2. 28
      MultiTerm.Wpf/App.xaml.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));
}

@ -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<ILogger>();
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;
// instanciate startup form and show
var startupForm = AppHost.Services.GetRequiredService<MainWindow>();
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<ILogger>();
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
}

Loading…
Cancel
Save