From a98f87a6307f52418096fde7a2297c12cd7f1d9f Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Wed, 22 Dec 2021 12:52:23 -0500 Subject: [PATCH] Add some basic logging --- App.xaml.cs | 10 ++++++++++ AudioWatcher.cs | 18 +++++++++++++++--- WorkIndicator.csproj | 6 ++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/App.xaml.cs b/App.xaml.cs index 80e10fe..2346580 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -1,6 +1,7 @@ using Common.Helpers; using Common.IO; using Common.Wpf.Extensions; +using Serilog; using Squirrel; using System; using System.Reflection; @@ -25,6 +26,13 @@ namespace WorkIndicator { SquirrelAwareApp.HandleEvents(onAppUpdate: version => Common.Settings.Extensions.RestoreSettings()); + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Debug() + .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day) + .CreateLogger(); + + Log.Logger.Debug($"Startup"); + var application = new App(); application.InitializeComponent(); application.Run(); @@ -90,6 +98,8 @@ namespace WorkIndicator protected override void OnExit(ExitEventArgs e) { + Log.Logger.Debug($"Exit"); + // Get rid of the light controller LightController.Dispose(); diff --git a/AudioWatcher.cs b/AudioWatcher.cs index d5d1c10..1a72cfb 100644 --- a/AudioWatcher.cs +++ b/AudioWatcher.cs @@ -1,4 +1,5 @@ using CSCore.CoreAudioAPI; +using Serilog; using System; using System.Collections.Generic; using System.Linq; @@ -20,8 +21,12 @@ namespace WorkIndicator private static readonly List _sessionManagers = new List(); + private static readonly ILogger Logger = Log.Logger; + public static void Start() { + Logger.Debug("AudioWatcher - Start"); + _manualResetEvent = new ManualResetEvent(false); _thread = new Thread(delegate () @@ -34,11 +39,9 @@ namespace WorkIndicator _sessionManagers.Add(sessionManager); - var sessionEnumerator = sessionManager.GetSessionEnumerator(); - sessionManager.SessionCreated += (sessionSender, sessionCreatedEventArgs) => HandleDeviceSession(device, sessionCreatedEventArgs.NewSession); - foreach (var audioSessionControl in sessionEnumerator) + foreach (var audioSessionControl in sessionManager.GetSessionEnumerator()) { HandleDeviceSession(device, audioSessionControl); } @@ -55,6 +58,8 @@ namespace WorkIndicator { var deviceId = device.DeviceID + audioSessionControl.GroupingParam; + Logger.Debug($"AudioWatcher - HandleDeviceSession - {device.FriendlyName}, {deviceId}"); + if (!ActiveSessions.ContainsKey(deviceId)) ActiveSessions[deviceId] = 0; @@ -66,6 +71,8 @@ namespace WorkIndicator public static void Stop() { + Logger.Debug("AudioWatcher - Stop"); + _sessionManagers.Clear(); _manualResetEvent?.Set(); @@ -73,6 +80,8 @@ namespace WorkIndicator private static void HandleAudioStateChanged(string deviceId, AudioSessionState newState) { + Logger.Debug($"AudioWatcher - HandleAudioStateChanged - {deviceId}, {newState}"); + switch (newState) { case AudioSessionState.AudioSessionStateActive: @@ -83,11 +92,14 @@ namespace WorkIndicator ActiveSessions[deviceId]--; break; case AudioSessionState.AudioSessionStateExpired: + ActiveSessions[deviceId] = 0; break; default: throw new ArgumentOutOfRangeException(); } + Logger.Debug($"AudioWatcher - HandleAudioStateChanged - {deviceId} = {ActiveSessions[deviceId]}"); + MicrophoneInUseChanged?.Invoke(MicrophoneInUse()); } diff --git a/WorkIndicator.csproj b/WorkIndicator.csproj index 7e42f81..930aaa6 100644 --- a/WorkIndicator.csproj +++ b/WorkIndicator.csproj @@ -238,6 +238,12 @@ 13.0.1 + + 2.10.0 + + + 5.0.0 + 2.0.1