예제 #1
0
        public static void SysInfo()
        {
            new Thread(async _ =>
            {
                double memPct = 0;
                ManagementObjectSearcher manageProcessor = new ManagementObjectSearcher("Select * from Win32_Processor");
                ManagementObjectSearcher manageRam       = new ManagementObjectSearcher("select * from Win32_OperatingSystem");
                int processorCores = manageProcessor.Get().Cast <ManagementBaseObject>().Sum(item => int.Parse(item["NumberOfLogicalProcessors"].ToString()));

                PerformanceCounter[] pcounters = new PerformanceCounter[processorCores];
                for (int i = 0; i < processorCores; i++)
                {
                    pcounters[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
                }

                while (true)
                {
                    try
                    {
                        SysInfoLogModel sysInfo = new SysInfoLogModel
                        {
                            CreatedDateTime = DateTime.Now,
                            MeasureInterval = LoggingInterval
                        };

                        double freeMem    = manageRam.Get().Cast <ManagementObject>().Select(m => double.Parse(m["FreePhysicalMemory"].ToString())).FirstOrDefault();
                        double visibleMem = manageRam.Get().Cast <ManagementObject>().Select(m => double.Parse(m["TotalVisibleMemorySize"].ToString())).FirstOrDefault();
                        memPct            = visibleMem == 0 ? 0 : (visibleMem - freeMem) / visibleMem * 100;

                        for (int i = 0; i < pcounters.Length; i++)
                        {
                            PerformanceCounter p           = pcounters[i];
                            sysInfo.CurrentProcessorUsage += p.NextValue().RoundUp();
                        }
                        sysInfo.CurrentProcessorUsage = (sysInfo.CurrentProcessorUsage / pcounters.Length).RoundUp();

                        sysInfo.CurrentMemoryUsage = memPct.RoundUp();

                        ManagementObject manageDisk   = new ManagementObject("Win32_PerfFormattedData_PerfDisk_PhysicalDisk.Name='0 C:'");
                        Dictionary <string, object> d = manageDisk.Properties.Cast <PropertyData>().ToDictionary(p => p.Name, p => p.Value);

                        sysInfo.AvgKBPerDiskWrite = d.TryGetValue("AvgDiskBytesPerWrite", out object avgBytes) ? double.Parse(avgBytes.ToString()) : 0;
                        sysInfo.CurrentDiskUsage  = d.TryGetValue("PercentDiskTime", out object diskPct) ? int.Parse(diskPct.ToString()) : 0;

                        sysInfo.CurrentNetworkUsage = NetworkByteLength.GetSumOfBag();
                        sysInfo.APICalls            = ApiCallBag.GetSumOfBag();
                        bool canInsert = sysInfo.CurrentProcessorUsage <= 100 && sysInfo.CurrentMemoryUsage <= 100 && sysInfo.CurrentDiskUsage <= 100;

                        if (canInsert && sysInfo.CurrentProcessorUsage > 0 && !LoggingIsPaused)
                        {
                            Task task = sysInfo.LogSystemInfo();
                            await task;
                        }
                    }
                    catch (Exception err)
                    {
                        await err.LogErrors("Worker", "SysInfo");
                    }
                    await Task.Delay(LoggingInterval);
                }
            })
            {
                IsBackground = true
            }.Start();