Skip to content

Commit

Permalink
refactor: add better logging when cancelling refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Belphemur committed Dec 13, 2023
1 parent 2df1449 commit 29f986b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 8 additions & 6 deletions SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ private void DeviceChanged(object sender, DeviceChangedEventBase e)

public void Refresh(CancellationToken cancellationToken = default)
{
var logContext = _context.ForContext("TaskId", Task.CurrentId);

Check failure on line 93 in SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs

View workflow job for this annotation

GitHub Actions / nightly-release

The name 'Task' does not exist in the current context
// Cancel the previous refresh operation, if any
var previousCts = Interlocked.Exchange(ref _refreshCancellationTokenSource, CancellationTokenSource.CreateLinkedTokenSource(cancellationToken));
if (previousCts != null)
{
logContext.Info("Cancelling Previous Context");

Check failure on line 98 in SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs

View workflow job for this annotation

GitHub Actions / nightly-release

'ILogger' does not contain a definition for 'Info' and no accessible extension method 'Info' accepting a first argument of type 'ILogger' could be found (are you missing a using directive or an assembly reference?)
previousCts.Cancel();
previousCts.Dispose();
}
Expand All @@ -108,13 +110,13 @@ public void Refresh(CancellationToken cancellationToken = default)

using var registration = cancellationToken.Register(_ =>
{
_context.Warning("Cancellation received.");
logContext.Warning("Cancellation received.");
throw new OperationCanceledException("Stop refreshing", cancellationToken);
}, null);

try
{
_context.Information("Refreshing all devices");
logContext.Information("Refreshing all devices");
var enumerator = new MMDeviceEnumerator();
using var _ = enumerator.DisposeOnCancellation(cancellationToken);
foreach (var endPoint in enumerator.EnumerateAudioEndPoints(DataFlow.All, _state))
Expand Down Expand Up @@ -144,7 +146,7 @@ public void Refresh(CancellationToken cancellationToken = default)
}
catch (Exception e)
{
_context.Warning(e, "Can't get name of device {device}", endPoint.ID);
logContext.Warning(e, "Can't get name of device {device}", endPoint.ID);
}
}

Expand All @@ -157,15 +159,15 @@ public void Refresh(CancellationToken cancellationToken = default)
RecordingDevices = recordingDevices.Values.ToArray();


_context.Information("Refreshed all devices in {@StopTime}. {@Recording}/rec, {@Playback}/play", stopWatch.Elapsed, recordingDevices.Count, playbackDevices.Count);
logContext.Information("Refreshed all devices in {@StopTime}. {@Recording}/rec, {@Playback}/play", stopWatch.Elapsed, recordingDevices.Count, playbackDevices.Count);
}
//If cancellation token is cancelled, its expected to throw null since the device enumerator has been disposed
catch (NullReferenceException e) when (!cancellationToken.IsCancellationRequested)
{
_context.Error(e, "Can't refresh the devices");
logContext.Error(e, "Can't refresh the devices");
} catch(NullReferenceException e) when (cancellationToken.IsCancellationRequested)
{
_context.Information(e, "Cancellation requested and enumerator is disposed, ignoring");
logContext.Information(e, "Cancellation requested and enumerator is disposed, ignoring");
}
}
finally
Expand Down
6 changes: 6 additions & 0 deletions SoundSwitch/Framework/Audio/Lister/Job/DebounceRefreshJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public Task OnFailure(JobException exception)
_logger.Warning("Refresh of device interrupted");
return Task.CompletedTask;
}

if(exception.InnerException is AggregateException aggregateException && aggregateException.InnerExceptions.FirstOrDefault() is OperationCanceledException)

Check failure on line 39 in SoundSwitch/Framework/Audio/Lister/Job/DebounceRefreshJob.cs

View workflow job for this annotation

GitHub Actions / nightly-release

'ReadOnlyCollection<Exception>' does not contain a definition for 'FirstOrDefault' and no accessible extension method 'FirstOrDefault' accepting a first argument of type 'ReadOnlyCollection<Exception>' could be found (are you missing a using directive or an assembly reference?)
{
_logger.Warning("Refresh of device interrupted");
return Task.CompletedTask;
}

_logger.Warning(exception, "Can't refresh devices");
return Task.CompletedTask;
Expand Down

0 comments on commit 29f986b

Please sign in to comment.