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 Jan 27, 2025
1 parent 4606937 commit 9029276
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 @@ -14,6 +14,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 @@ -268,6 +269,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 @@ -413,6 +428,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 @@ -498,6 +516,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 @@ -511,7 +534,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 @@ -537,8 +568,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 @@ -550,8 +591,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 @@ -563,8 +614,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 @@ -576,8 +642,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 decreasing volume volume."
);
throw;
}
})
.Join(_taskCancellationTokenSource.Token)
);
Expand All @@ -588,8 +669,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 @@ -33,6 +33,8 @@
<ItemGroup>
<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 9029276

Please sign in to comment.