From 5e58fb3aadafe70762199bde0beef9a3759fefd1 Mon Sep 17 00:00:00 2001 From: sleevezipper Date: Sat, 6 Mar 2021 15:39:17 +0100 Subject: [PATCH] remove extra logging and ignore service sessions if WMI class not found --- .../Domain/Sensors/SessionStateSensor.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs b/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs index 1fb5990..882cbd7 100644 --- a/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs +++ b/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs @@ -68,12 +68,10 @@ namespace hass_workstation_service.Domain.Sensors var scope = new ManagementScope(); scope.Connect(); - Log.Logger.Information("Connected to Scope"); + var explorerProcesses = Process.GetProcessesByName("explorer") .Select(p => p.Id.ToString()) .ToHashSet(); - - Log.Logger.Information("Created explorerprocess list"); var REprocessid = new Regex(@"(?<=Handle="").*?(?="")", RegexOptions.Compiled); var numberOfLogonSessionsWithExplorer = new ManagementObjectSearcher(scope, new SelectQuery("SELECT * FROM Win32_SessionProcess")).Get() @@ -82,18 +80,20 @@ namespace hass_workstation_service.Domain.Sensors .Select(mo => mo["Antecedent"].ToString()) .Distinct() .Count(); + int numberOfUserDesktops = 1; - - Log.Logger.Information("Created numberOfLogonSessionsWithExplorer"); - - var numberOfUserDesktops = new ManagementObjectSearcher(scope, new SelectQuery("select * from win32_Perfrawdata_TermService_TerminalServicesSession")).Get().Count - 1; // don't count Service desktop - - Log.Logger.Information("Created numberOfUserDesktops"); + // this can fail sometimes, that's why we set numberOfUserDesktops to 1 + try + { + numberOfUserDesktops = new ManagementObjectSearcher(scope, new SelectQuery("select * from win32_Perfrawdata_TermService_TerminalServicesSession")).Get().Count - 1; // don't count Service desktop + } + catch + { + + } var numberOflogonUIProcesses = Process.GetProcessesByName("LogonUI").Length; - Log.Logger.Information("Created numberOflogonUIProcesses"); - if (numberOflogonUIProcesses >= numberOfUserDesktops) { if (numberOfLogonSessionsWithExplorer > 0)