From 2e14ff032bfaf22af5e584bdf849614ad940e72e Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Tue, 27 Jan 2026 19:59:51 -0500 Subject: [PATCH] Adjust logging and add debugger option --- HardwareMonitorStatusWindow.sln.DotSettings | 3 +- Service/Program.cs | 36 +++++++++++-------- StatusWindow/Program.cs | 16 +++++---- StatusWindow/SerilogVelopackLogger.cs | 18 ++++++++++ StatusWindow/UpdateCheck.cs | 4 +-- StatusWindow/WindowSource.cs | 39 +++++++++++++++------ 6 files changed, 81 insertions(+), 35 deletions(-) create mode 100644 StatusWindow/SerilogVelopackLogger.cs diff --git a/HardwareMonitorStatusWindow.sln.DotSettings b/HardwareMonitorStatusWindow.sln.DotSettings index b347252..27d1c29 100644 --- a/HardwareMonitorStatusWindow.sln.DotSettings +++ b/HardwareMonitorStatusWindow.sln.DotSettings @@ -1,3 +1,4 @@  True - True \ No newline at end of file + True + True \ No newline at end of file diff --git a/Service/Program.cs b/Service/Program.cs index 4e4b9e2..d8b05f1 100644 --- a/Service/Program.cs +++ b/Service/Program.cs @@ -14,11 +14,11 @@ internal class Program { Log.Logger = new LoggerConfiguration().WriteTo.File("HardwareMonitorService.log").CreateLogger(); - Log.Logger.Information("Start"); + Log.Information("Start"); if (args.Contains("--install", StringComparer.InvariantCultureIgnoreCase)) { - Log.Logger.Information("Starting install..."); + Log.Information("Starting install..."); try { @@ -48,14 +48,14 @@ internal class Program } catch (Exception exception) { - Log.Logger.Error(exception, "Install"); + Log.Error(exception, ""); } - Log.Logger.Information("Install complete"); + Log.Information("Install complete"); } else if (args.Contains("--uninstall", StringComparer.InvariantCultureIgnoreCase)) { - Log.Logger.Information("Starting uninstall..."); + Log.Information("Starting uninstall..."); try { @@ -69,43 +69,51 @@ internal class Program } catch (Exception exception) { - Log.Logger.Error(exception, "Uninstall"); + Log.Error(exception, ""); } - Log.Logger.Information("Uninstall complete"); + Log.Information("Uninstall complete"); } else { - Log.Logger.Information("Starting"); + Log.Information("Starting"); try { while (true) { + Log.Information("Creating PipeSecurity"); + var pipeSecurity = new PipeSecurity(); pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow)); + Log.Information("Creating NamedPipe"); + var pipeWithSecurity = NamedPipeServerStreamAcl.Create(HardwareMonitorService.PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 0, 0, pipeSecurity); + Log.Information("Creating PipeServer"); + var pipeServer = new PipeServer(new HardwarePipeSerializer(), pipeWithSecurity, () => new HardwareMonitorService()); - //var pipeServer = new PipeServer( - // new HardwarePipeSerializer(), - // HardwareMonitorService.PipeName, - // () => new HardwareMonitorService()); + + Log.Information("Waiting for connection"); await pipeServer.WaitForConnectionAsync().ConfigureAwait(false); + Log.Information("Waiting for remote pipe to close"); + await pipeServer.WaitForRemotePipeCloseAsync().ConfigureAwait(false); + Log.Information("Disposing pipe server"); + pipeServer.Dispose(); } } catch (Exception exception) { - Log.Logger.Error(exception, ""); + Log.Error(exception, ""); } } - Log.Logger.Information("Closing"); + Log.Information("Closing"); } } \ No newline at end of file diff --git a/StatusWindow/Program.cs b/StatusWindow/Program.cs index def11dc..e7949d6 100644 --- a/StatusWindow/Program.cs +++ b/StatusWindow/Program.cs @@ -1,5 +1,6 @@ -using System; -using Serilog; +using Serilog; +using System; +using System.Diagnostics; using Velopack; namespace HardwareMonitorStatusWindow.StatusWindow; @@ -9,18 +10,19 @@ internal class Program [STAThread] public static void Main(string[] args) { + if (args.Contains("--debug")) + Debugger.Launch(); + Log.Logger = new LoggerConfiguration().WriteTo.File("HardwareMonitorStatusWindow.log").CreateLogger(); - Log.Logger.Information("Start"); + Log.Information("Start"); - // var loggerFactory = new LoggerFactory().AddSerilog(Log.Logger); - - VelopackApp.Build().Run(); // loggerFactory.CreateLogger("Install") + VelopackApp.Build().SetLogger(new SerilogVelopackLogger()).Run(); var app = new App(); app.InitializeComponent(); app.Run(); - Log.Logger.Information("End"); + Log.Information("End"); } } \ No newline at end of file diff --git a/StatusWindow/SerilogVelopackLogger.cs b/StatusWindow/SerilogVelopackLogger.cs new file mode 100644 index 0000000..77b025d --- /dev/null +++ b/StatusWindow/SerilogVelopackLogger.cs @@ -0,0 +1,18 @@ +using System; +using Velopack.Logging; + +namespace HardwareMonitorStatusWindow.StatusWindow; + +internal class SerilogVelopackLogger : IVelopackLogger +{ + public void Log(VelopackLogLevel logLevel, string? message, Exception? exception) + { + if (exception != null) + { + Serilog.Log.Error(exception, ""); + return; + } + + Serilog.Log.Information("[{time}] [{logLevel}] {message}", DateTime.Now.ToShortTimeString(), logLevel, message); + } +} \ No newline at end of file diff --git a/StatusWindow/UpdateCheck.cs b/StatusWindow/UpdateCheck.cs index 415a35c..e53010d 100644 --- a/StatusWindow/UpdateCheck.cs +++ b/StatusWindow/UpdateCheck.cs @@ -30,11 +30,11 @@ internal static class UpdateCheck if (MessageBox.Show(updateCheckMessage, updateCheckTitle, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) return; - Log.Logger.Information("Downloading update"); + Log.Information("Downloading update"); await UpdateManager.DownloadUpdatesAsync(newVersion); - Log.Logger.Information("Installing update"); + Log.Information("Installing update"); UpdateManager.ApplyUpdatesAndRestart(newVersion); } diff --git a/StatusWindow/WindowSource.cs b/StatusWindow/WindowSource.cs index d924e3b..ddb42fa 100644 --- a/StatusWindow/WindowSource.cs +++ b/StatusWindow/WindowSource.cs @@ -30,18 +30,26 @@ internal class WindowSource : IWindowSource, IDisposable { using var taskService = new TaskService(); + Log.Information("Checking for task name: {name}", HardwareMonitorService.ScheduledTaskName); + var existingTask = taskService.FindTask(HardwareMonitorService.ScheduledTaskName); + Log.Information("Task: {existingTask}", existingTask); + if (existingTask == null) { var assembly = Assembly.GetExecutingAssembly(); var path = Path.GetDirectoryName(assembly.Location); + Log.Information("Service path: {path}", path); + if (path != null) { var fileName = Path.Combine(path, "HardwareMonitorService.exe"); + Log.Information("Full service path: {fileName}", fileName); + var startInfo = new ProcessStartInfo { FileName = fileName, @@ -50,13 +58,16 @@ internal class WindowSource : IWindowSource, IDisposable Verb = "runas" }; + Log.Information("Starting process"); + Process.Start(startInfo); } } } - catch (Exception) + catch (Exception exception) { // Ignored + Log.Error(exception, ""); } _floatingStatusWindow = new FloatingStatusWindow(this); @@ -73,36 +84,42 @@ internal class WindowSource : IWindowSource, IDisposable { try { + Log.Information("IsInstalled: {isInstalled}", UpdateCheck.IsInstalled); + if (!UpdateCheck.IsInstalled) return false; + Log.Information("CheckVersionAtStartup: {checkVersionAtStartup}", Settings.Default.CheckVersionAtStartup); + if (!Settings.Default.CheckVersionAtStartup) return false; - Log.Logger.Information("Checking for update"); - await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText(Resources.CheckingForUpdate)); + Log.Information("Checking for updates"); + var newVersion = await UpdateCheck.UpdateManager.CheckForUpdatesAsync(); + Log.Information("New version: {version}", newVersion); + if (newVersion == null) return false; - Log.Logger.Information("Downloading update"); - await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText(Resources.DownloadingUpdate)); + Log.Information("Downloading update"); + await UpdateCheck.UpdateManager.DownloadUpdatesAsync(newVersion); - Log.Logger.Information("Installing update"); - await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText(Resources.InstallingUpdate)); + Log.Information("Installing update"); + UpdateCheck.UpdateManager.ApplyUpdatesAndRestart(newVersion); } catch (Exception e) { - Log.Logger.Error(e, nameof(UpdateApp)); + Log.Error(e, nameof(UpdateApp)); } return true; @@ -110,16 +127,16 @@ internal class WindowSource : IWindowSource, IDisposable private async Task Start(bool hasUpdate) { - Log.Logger.Information("Start: hasUpdate={hasUpdate}", hasUpdate); + Log.Information("Start: hasUpdate={hasUpdate}", hasUpdate); if (hasUpdate) return; - Log.Logger.Information("Load"); + Log.Information("Load"); await Load(); - Log.Logger.Information("Starting timer"); + Log.Information("Starting timer"); _timer.Elapsed += HandleTimerElapsed; _timer.AutoReset = false;