Skip to content

Commit

Permalink
Implement logging when unexpected errors occur.
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimian committed Feb 2, 2025
1 parent a4c8050 commit 5385478
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 11 deletions.
113 changes: 102 additions & 11 deletions src/TotalMixVC/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();
}

/// <summary>Disposes the current app.</summary>
Expand Down Expand Up @@ -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;
}
}
}

Expand All @@ -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
Expand All @@ -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)
);
Expand All @@ -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)
);
Expand All @@ -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)
);
Expand All @@ -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)
);
Expand All @@ -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)
);
Expand Down
2 changes: 2 additions & 0 deletions src/TotalMixVC/TotalMixVC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<PackageReference Include="AsyncAwaitBestPractices.MVVM" Version="9.0.0" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="2.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.12.19" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 5385478

Please sign in to comment.