Detailed introduction to code examples of performance counters for C# server performance monitoring

黄舟
Release: 2017-03-14 14:01:07
Original
2883 people have browsed it

The previous article "Server Performance Monitoring - WMI" introduced the acquisition of server performance through remote com (of course it can also be used locally), so this article mainly talks about the performance monitoring function that comes with the Windows system-----> ;performancecouonter.

Open the management tool-->Performance, we can immediately see the trend chart of the server's CPU, process running time, disk capacity and other performance parameters. However, it is not just these items. We can view other performance indicators by adding technical tools:

If you say that it is too troublesome to look at it this way, OK, we pass C#Take these values ​​out and use them to implement your own performance monitoring:

1. Add reference:

using System.Diagnostics;
Copy after login

2. Create and instance PerformanceCounter

public static System.Diagnostics.PerformanceCounter pc= new System.Diagnostics.PerformanceCounter();
public static System.Diagnostics.PerformanceCounter pcm= new System.Diagnostics.PerformanceCounter();
public static System.Diagnostics.PerformanceCounter pcb= new System.Diagnostics.PerformanceCounter();
public static System.Diagnostics.PerformanceCounter pcc= new System.Diagnostics.PerformanceCounter();
//我们用四个对象做不同的操作,注意:是static的,不然每次取出的数据都是初始值,如cpu利用率就是0
Copy after login

3.Constructor

static CapabilityScout()
...{
pc.CategoryName = "Processor";
pc.CounterName = "% Processor Time";
pc.InstanceName = "_Total";
pc.MachineName = ".";
pcm.CategoryName = "Memory";
pcm.CounterName = "% Committed Bytes In Use";
pcm.MachineName = ".";
pcb.CategoryName = "Windows Media Unicast Service";
pcb.CounterName = "Allocated Bandwidth";
pcb.MachineName = ".";
pcc.CategoryName = "Windows Media Unicast Service";
pcc.CounterName = "Connected Clients";
pcc.MachineName = ".";
}
Copy after login

4. Get the counter value

    获取CPU利用率#region 获取CPU利用率
        public static string getCpuUsage()
        ...{
            string used = pc.NextValue().ToString();
            return used;
        }
        #endregion
        获取内存使用率#region 获取内存使用率
        public static string getMemory()
        ...{
            float used = pcm.NextValue();
            return used.ToString();
        }
        #endregion
        获取WMS连接数#region 获取WMS连接数
        public static string getConnectedCount()
        ...{
            string count = pcc.NextValue().ToString();
            return count;
        }
        #endregion
        获取网络流量#region 获取网络流量
        public static string getServerBandWidth()
        ...{
            string bandwidth = pcb.NextValue().ToString();
            return bandwidth;
        }
        #endregion
Copy after login

Of course, here is just There are only a few of them, but by using the same method, we can get more performance and process running conditions. But one thing to note is that the data obtained must be provided by the windows service. Of course, we can also get it ourselves Writing some windows services and adding them to the system performance counter is also very convenient for .net.

How about it? Compared with WMI, is it more convenient? Haha~~

The above is the detailed content of Detailed introduction to code examples of performance counters for C# server performance monitoring. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!