From 538547863814335ce607e406d774e7823629d6b8 Mon Sep 17 00:00:00 2001 From: Fotis Gimian Date: Sun, 25 Jun 2023 23:29:49 +1000 Subject: [PATCH] Implement logging when unexpected errors occur. --- src/TotalMixVC/App.xaml.cs | 113 ++++++++++++++++++++++++++++--- src/TotalMixVC/TotalMixVC.csproj | 2 + 2 files changed, 104 insertions(+), 11 deletions(-) diff --git a/src/TotalMixVC/App.xaml.cs b/src/TotalMixVC/App.xaml.cs index aaebcfd..7730f67 100644 --- a/src/TotalMixVC/App.xaml.cs +++ b/src/TotalMixVC/App.xaml.cs @@ -12,6 +12,7 @@ using System.Windows.Media; using Hardcodet.Wpf.TaskbarNotification; using Microsoft.VisualStudio.Threading; +using Serilog; using TotalMixVC.Communicator; using TotalMixVC.Configuration; using TotalMixVC.Hotkeys; @@ -252,6 +253,20 @@ protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); + // Configure logging. + var appSettingsPath = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + "TotalMix Volume Control" + ); + Directory.CreateDirectory(appSettingsPath); + Log.Logger = new LoggerConfiguration() + .WriteTo.File( + Path.Combine(appSettingsPath, "app.log"), + formatProvider: CultureInfo.InvariantCulture + ) + .CreateLogger(); + Log.Information("Starting up the application"); + // Create a cancellation token source to allow cancellation of tasks on exit. _taskCancellationTokenSource = new(); @@ -390,6 +405,9 @@ protected override async void OnExit(ExitEventArgs e) // Dispose any objects which implement the IDisposable interface. Dispose(); + + // Close the log. + Log.CloseAndFlush(); } /// Disposes the current app. @@ -507,6 +525,11 @@ await _joinableTaskFactory.SwitchToMainThreadAsync( // This exception is raised when the app is exited so we exit the loop. break; } + catch (Exception e) + { + Log.Error(e, "Unhandled exception caught while receiving volume."); + throw; + } } } @@ -520,7 +543,15 @@ private async Task RequestVolumeAsync() // The volume is uninitialized so it is requested from the device. if (!_volumeManager.IsVolumeInitialized) { - await _volumeManager.RequestVolumeAsync().ConfigureAwait(false); + try + { + await _volumeManager.RequestVolumeAsync().ConfigureAwait(false); + } + catch (Exception e) + { + Log.Error(e, "Unhandled exception caught while requesting volume."); + throw; + } } // A volume request was just sent or the volume is already known, so we sleep @@ -546,8 +577,18 @@ private void RegisterHotkeys() .RunAsync(async () => { // Increase the volume and show the volume indicator. - await _volumeManager.IncreaseVolumeAsync().ConfigureAwait(false); - await _volumeIndicator.DisplayCurrentVolumeAsync().ConfigureAwait(false); + try + { + await _volumeManager.IncreaseVolumeAsync().ConfigureAwait(false); + await _volumeIndicator + .DisplayCurrentVolumeAsync() + .ConfigureAwait(false); + } + catch (Exception e) + { + Log.Error(e, "Unhandled exception caught while increasing volume."); + throw; + } }) .Join(_taskCancellationTokenSource.Token) ); @@ -559,8 +600,18 @@ private void RegisterHotkeys() .RunAsync(async () => { // Decrease the volume and show the volume indicator. - await _volumeManager.DecreaseVolumeAsync().ConfigureAwait(false); - await _volumeIndicator.DisplayCurrentVolumeAsync().ConfigureAwait(false); + try + { + await _volumeManager.DecreaseVolumeAsync().ConfigureAwait(false); + await _volumeIndicator + .DisplayCurrentVolumeAsync() + .ConfigureAwait(false); + } + catch (Exception e) + { + Log.Error(e, "Unhandled exception caught while decreasing volume."); + throw; + } }) .Join(_taskCancellationTokenSource.Token) ); @@ -572,8 +623,23 @@ private void RegisterHotkeys() .RunAsync(async () => { // Finely increase the volume and show the volume indicator. - await _volumeManager.IncreaseVolumeAsync(fine: true).ConfigureAwait(false); - await _volumeIndicator.DisplayCurrentVolumeAsync().ConfigureAwait(false); + try + { + await _volumeManager + .IncreaseVolumeAsync(fine: true) + .ConfigureAwait(false); + await _volumeIndicator + .DisplayCurrentVolumeAsync() + .ConfigureAwait(false); + } + catch (Exception e) + { + Log.Error( + e, + "Unhandled exception caught while fine increasing volume." + ); + throw; + } }) .Join(_taskCancellationTokenSource.Token) ); @@ -585,8 +651,23 @@ private void RegisterHotkeys() .RunAsync(async () => { // Finely decrease the volume and show the volume indicator. - await _volumeManager.DecreaseVolumeAsync(fine: true).ConfigureAwait(false); - await _volumeIndicator.DisplayCurrentVolumeAsync().ConfigureAwait(false); + try + { + await _volumeManager + .DecreaseVolumeAsync(fine: true) + .ConfigureAwait(false); + await _volumeIndicator + .DisplayCurrentVolumeAsync() + .ConfigureAwait(false); + } + catch (Exception e) + { + Log.Error( + e, + "Unhandled exception caught while fine decreasing volume." + ); + throw; + } }) .Join(_taskCancellationTokenSource.Token) ); @@ -597,8 +678,18 @@ private void RegisterHotkeys() _joinableTaskFactory .RunAsync(async () => { - await _volumeManager.ToggloDimAsync().ConfigureAwait(false); - await _volumeIndicator.DisplayCurrentVolumeAsync().ConfigureAwait(false); + try + { + await _volumeManager.ToggloDimAsync().ConfigureAwait(false); + await _volumeIndicator + .DisplayCurrentVolumeAsync() + .ConfigureAwait(false); + } + catch (Exception e) + { + Log.Error(e, "Unhandled exception caught while toggling dim."); + throw; + } }) .Join(_taskCancellationTokenSource.Token) ); diff --git a/src/TotalMixVC/TotalMixVC.csproj b/src/TotalMixVC/TotalMixVC.csproj index 02438b6..2fd60ce 100644 --- a/src/TotalMixVC/TotalMixVC.csproj +++ b/src/TotalMixVC/TotalMixVC.csproj @@ -34,6 +34,8 @@ + +