using System; using System.Diagnostics; using System.Management; using ATxCommon; using NLog; using NLog.Config; using NLog.Targets; namespace ATxDiagnostics { class ATxDiagnostics { private static readonly Logger Log = LogManager.GetCurrentClassLogger(); static void Main(string[] args) { var loglevel = LogLevel.Debug; if (args.Length > 0 && args[0] == "trace") { loglevel = LogLevel.Trace; } var logConfig = new LoggingConfiguration(); var logTargetConsole = new ConsoleTarget { Name = "console", Header = "AutoTx Diagnostics", Layout = @"${time} [${level}] ${message}", }; logConfig.AddTarget(logTargetConsole); var logRuleConsole = new LoggingRule("*", loglevel, logTargetConsole); logConfig.LoggingRules.Add(logRuleConsole); LogManager.Configuration = logConfig; Log.Debug(Conv.BytesToString(SystemChecks.GetFreeDriveSpace("C:"))); GetCpuCounter(); /* 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 watch = Stopwatch.StartNew(); var queryString = "SELECT Name, PercentProcessorTime " + "FROM Win32_PerfFormattedData_PerfOS_Processor"; var opts = new EnumerationOptions { Timeout = new TimeSpan(0, 0, 10), ReturnImmediately = false, }; var searcher = new ManagementObjectSearcher("", queryString, opts); Int32 usageInt32 = -1; var managementObjects = searcher.Get(); if (managementObjects.Count == 0) { Log.Error("No objects returned from WMI!"); watch.Stop(); Log.Debug("WMI query took {0} ms.", watch.ElapsedMilliseconds); return -1; } 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); } managementObjects.Dispose(); searcher.Dispose(); watch.Stop(); Log.Debug("WMI query took {0} ms.", watch.ElapsedMilliseconds); return usageInt32; } private static void GetCpuCounter() { var counter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); var i = 0; while (true) { i++; //Log.Debug("PerformanceCounter reading {1}: {0}", counter.NextValue(), i); var watch = Stopwatch.StartNew(); var reading = counter.NextValue(); watch.Stop(); Console.WriteLine("reading {1} (took {2} ms): {0}", reading, i, watch.ElapsedMilliseconds); System.Threading.Thread.Sleep(250); } } } }