remove extra logging and ignore service sessions if WMI class not found

13-speakers-in-use
sleevezipper 4 years ago
parent 451c4bef30
commit 5e58fb3aad

@ -68,12 +68,10 @@ namespace hass_workstation_service.Domain.Sensors
var scope = new ManagementScope(); var scope = new ManagementScope();
scope.Connect(); scope.Connect();
Log.Logger.Information("Connected to Scope");
var explorerProcesses = Process.GetProcessesByName("explorer") var explorerProcesses = Process.GetProcessesByName("explorer")
.Select(p => p.Id.ToString()) .Select(p => p.Id.ToString())
.ToHashSet(); .ToHashSet();
Log.Logger.Information("Created explorerprocess list");
var REprocessid = new Regex(@"(?<=Handle="").*?(?="")", RegexOptions.Compiled); var REprocessid = new Regex(@"(?<=Handle="").*?(?="")", RegexOptions.Compiled);
var numberOfLogonSessionsWithExplorer = new ManagementObjectSearcher(scope, new SelectQuery("SELECT * FROM Win32_SessionProcess")).Get() 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()) .Select(mo => mo["Antecedent"].ToString())
.Distinct() .Distinct()
.Count(); .Count();
int numberOfUserDesktops = 1;
// this can fail sometimes, that's why we set numberOfUserDesktops to 1
Log.Logger.Information("Created numberOfLogonSessionsWithExplorer"); try
{
var numberOfUserDesktops = new ManagementObjectSearcher(scope, new SelectQuery("select * from win32_Perfrawdata_TermService_TerminalServicesSession")).Get().Count - 1; // don't count Service desktop 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"); catch
{
}
var numberOflogonUIProcesses = Process.GetProcessesByName("LogonUI").Length; var numberOflogonUIProcesses = Process.GetProcessesByName("LogonUI").Length;
Log.Logger.Information("Created numberOflogonUIProcesses");
if (numberOflogonUIProcesses >= numberOfUserDesktops) if (numberOflogonUIProcesses >= numberOfUserDesktops)
{ {
if (numberOfLogonSessionsWithExplorer > 0) if (numberOfLogonSessionsWithExplorer > 0)

Loading…
Cancel
Save