Skip to content

Commit

Permalink
Updates for debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyts committed Jul 28, 2021
1 parent 37f39a0 commit 0b7623d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 36 deletions.
97 changes: 63 additions & 34 deletions SelfServiceSessionReset/Controllers/UserProcessController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Version 1.3
// Version 1.4
// Written by Jeremy Saunders ([email protected]) 13th June 2020
// Modified by Jeremy Saunders ([email protected]) 25th July 2021
// Modified by Jeremy Saunders ([email protected]) 28th July 2021
//
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -141,6 +141,10 @@ private List<UserProcess> GetRemoteUserProcesses(string remotehost, string usern
{
result = "Exception occured in an attempt to connecct to " + remotehost + " to get all processes for " + username + ": " + e.Message;
}
if (!string.IsNullOrEmpty(result))
{
Log.Debug(result);
}
return userprocesses;
}

Expand Down Expand Up @@ -240,7 +244,7 @@ private string BytesToReadableValue(long number)
/// <returns></returns>
private ExpandoObject GetPerfMonCounterInformation(string processName, int processId, string remotehost)
{
string result = "";
string result = string.Empty;
// Get the number of logical processors from the remote host
int logicalprocessors = GetLogicalProcessors(remotehost);
string processCounterName = "% Processor Time";
Expand All @@ -265,7 +269,10 @@ private ExpandoObject GetPerfMonCounterInformation(string processName, int proce
response.CpuUsagePercentage = Math.Round(counter.NextValue() / logicalprocessors, 2);
result = counter.InstanceName + " - Cpu: " + response.CpuUsagePercentage;
}

if (!string.IsNullOrEmpty(result))
{
Log.Debug(result);
}
return response;
}

Expand Down Expand Up @@ -296,7 +303,7 @@ private PerformanceCounter GetPerfCounterForProcessId(string processName, int pr
/// <returns></returns>
private string GetInstanceNameForProcessId(string processName, int processId, string remotehost)
{
string result = "";
string result = string.Empty;
try
{
// Create the appropriate PerformanceCounterCategory object.
Expand All @@ -323,6 +330,10 @@ private string GetInstanceNameForProcessId(string processName, int processId, st
{
result = "Exception occured in an attempt to get the performce data of the instance for the " + processId + ": " + e.Message;
}
if (!string.IsNullOrEmpty(result))
{
Log.Debug(result);
}
return null;
}

Expand All @@ -333,35 +344,51 @@ private string GetInstanceNameForProcessId(string processName, int processId, st
/// <returns></returns>
private int GetLogicalProcessors(string remotehost)
{
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", remotehost), connOptions);
manScope.Connect();

// Query the Win32_Processor for NumberOfLogicalProcessors
SelectQuery query = new SelectQuery("Select NumberOfLogicalProcessors from Win32_Processor");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(manScope, query);
ManagementObjectCollection processorInformation = searcher.Get();
string result = string.Empty;
int processorCount = 0;
try
{
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", remotehost), connOptions);
manScope.Connect();

int result = 0;
// Query the Win32_Processor for NumberOfLogicalProcessors
SelectQuery query = new SelectQuery("Select NumberOfLogicalProcessors from Win32_Processor");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(manScope, query);
ManagementObjectCollection processorInformation = searcher.Get();

foreach (ManagementObject obj in processorInformation)
{
// Retrieve the number of logical processors
if (obj["NumberOfLogicalProcessors"] != null)
foreach (ManagementObject obj in processorInformation)
{
try
// Retrieve the number of logical processors
if (obj["NumberOfLogicalProcessors"] != null)
{
int.TryParse(obj["NumberOfLogicalProcessors"].ToString(), out result);
try
{
int.TryParse(obj["NumberOfLogicalProcessors"].ToString(), out processorCount);
result = "The number of logical processors on " + remotehost + " is " + processorCount;
}
catch
{
result = "Unable to retrieve the number of logical processors on " + remotehost;
}
}
catch
else
{
//
result = "Unable to find the number of logical processors on " + remotehost;
}
}
}
return result;
catch (Exception e)
{
result = "Exception occured in an attempt to get the number of logical processors on " + remotehost + ": " + e.Message;
}
if (!string.IsNullOrEmpty(result))
{
Log.Debug(result);
}
return processorCount;
}

/// <summary>
Expand All @@ -388,6 +415,7 @@ private string TerminateRemoteProcess(string remotehost, int processId, string u
ManagementObjectCollection processList = searcher.Get();
foreach (ManagementObject proc in processList)
{
string procName = proc["Name"].ToString();
string procOwner = "None";
string[] Args = new string[] { "", "" };
int ReturnCode = Convert.ToInt32(proc.InvokeMethod("GetOwner", Args));
Expand Down Expand Up @@ -419,40 +447,41 @@ private string TerminateRemoteProcess(string remotehost, int processId, string u
switch (returnvalue)
{
case 0:
result = "Successful completion";
result = "Successful termination of the " + procName + " process.";
break;
case 2:
result = "Access denied";
result = "Access denied terminating the " + procName + " process.";
break;
case 3:
result = "Insufficient privilege";
result = "Insufficient privileges to terminate the " + procName + " process.";
break;
case 8:
result = "Unknown failure";
result = "Unknown failure terminating the " + procName + " process.";
break;
case 9:
result = "Path not found";
result = "Path not found to the " + procName + " process.";
break;
case 21:
result = "Invalid parameter";
result = "Invalid parameter supplied to terminate the " + procName + " process.";
break;
default:
result = "Terminate failed with error code " + outParams["ReturnValue"].ToString();
result = "Attempt to terminate the " + procName + " process failed with error code " + outParams["ReturnValue"].ToString() + ".";
break;
}
}
else
{
result = "Cannot terminate this process because " + username + " is not the process owner";
result = "Cannot terminate the " + procName + " process with a Process ID of " + processId + " because " + username + " is not the process owner.";
}
}
manScope = null;
connOptions = null;
}
catch (ManagementException e)
{
result = "Exception occured in an attempt to terminate Process ID " + processId + ": " + e.Message;
result = "Exception occured in an attempt to terminate Process ID " + processId + " for " + username + ": " + e.Message;
}
Log.Debug(result);
return result;
}

Expand Down
4 changes: 2 additions & 2 deletions SelfServiceSessionReset/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<add key="serilog:using:File" value="Serilog.Sinks.File" />
<add key="serilog:write-to:File.path" value="%BASEDIR%\log.txt" />
<add key="serilog:write-to:File.fileSizeLimitBytes" value="1234567" />
<add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="32" />
<add key="serilog:write-to:File.rollingInterval" value="Day" />
<add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="12" />
<add key="serilog:write-to:File.rollingInterval" value="Month" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
Expand Down

0 comments on commit 0b7623d

Please sign in to comment.