Skip to content
Snippets Groups Projects
Commit 2eef7120 authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Add a local WMI query method to ATxDiagnostics.

parent e3912d8d
No related branches found
No related tags found
No related merge requests found
using System;
using System.Management;
using ATxCommon;
using NLog;
using NLog.Config;
......@@ -15,13 +16,59 @@ namespace ATxDiagnostics
var logTargetConsole = new ConsoleTarget {
Name = "console",
Header = "test-header",
Layout = @"${date:format=yyyy-MM-dd HH\:mm\:ss} [${level}] (${logger}) ${message}",
};
logConfig.AddTarget(logTargetConsole);
var logRuleConsole = new LoggingRule("*", LogLevel.Trace, logTargetConsole);
logConfig.LoggingRules.Add(logRuleConsole);
LogManager.Configuration = logConfig;
Console.WriteLine(SystemChecks.WmiSummary());
Log.Debug(Conv.BytesToString(SystemChecks.GetFreeDriveSpace("C:")));
/*
Log.Debug(SystemChecks.GetCpuUsage());
Log.Debug(SystemChecks.GetCpuUsage());
Log.Debug(SystemChecks.GetCpuUsage());
Log.Debug(SystemChecks.GetCpuUsage());
Log.Debug(SystemChecks.GetCpuUsage());
Log.Debug(SystemChecks.GetCpuUsage());
*/
Log.Debug(QueryCpuLoad());
}
private static int QueryCpuLoad() {
Log.Trace("Querying WMI for CPU load...");
var searcher = new ManagementObjectSearcher(
"root\\CIMV2",
"select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor",
new EnumerationOptions {
Timeout = new TimeSpan(0, 0, 10),
ReturnImmediately = false,
}
);
Int32 usageInt32 = -1;
var managementObjects = searcher.Get();
Log.Trace("WMI query returned {0} objects.", managementObjects.Count);
foreach (var mo in managementObjects) {
var obj = (ManagementObject)mo;
var usage = obj["PercentProcessorTime"];
var name = obj["Name"];
usageInt32 = Convert.ToInt32(usage);
Log.Trace("CPU usage {1}: {0}", usageInt32, name);
//if (name.ToString().Equals("_Total")) {
// usageInt32 = Convert.ToInt32(usage);
// Log.Trace("CPU usage: {0}", usageInt32);
//}
}
managementObjects.Dispose();
searcher.Dispose();
return usageInt32;
}
}
}
......@@ -44,6 +44,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment