服务器运行过程中,实时掌握CPU、内存、磁盘等资源的占用情况,能够及时排查性能异常,避免服务宕机。C#自带的性能计数器类库可以方便对接系统底层的性能数据,无需额外引入第三方组件,就能实现轻量的服务器性能监控。

性能计数器核心类介绍
C#中操作性能计数器主要依赖System.Diagnostics命名空间下的PerformanceCounter类,该类可以读取或写入系统定义的性能计数器数据。使用前需要先引入对应命名空间:
using System.Diagnostics; using System.Collections.Generic;
常用性能指标采集示例
1. CPU使用率监控
CPU使用率对应的性能计数器类别为Processor,计数器名称为% Processor Time,实例名通常选择_Total表示整体CPU占用:
public class CpuMonitor
{
private PerformanceCounter cpuCounter;
public CpuMonitor()
{
// 初始化CPU性能计数器
cpuCounter = new PerformanceCounter(
categoryName: "Processor",
counterName: "% Processor Time",
instanceName: "_Total"
);
// 第一次调用NextValue会返回0,需要先调用一次初始化
cpuCounter.NextValue();
}
public float GetCpuUsage()
{
// 返回当前CPU使用率,单位为百分比
return cpuCounter.NextValue();
}
}2. 内存可用量监控
可用内存对应的类别为Memory,计数器名称为Available MBytes,直接读取即可获取当前系统可用的物理内存大小:
public class MemoryMonitor
{
private PerformanceCounter memoryCounter;
public MemoryMonitor()
{
memoryCounter = new PerformanceCounter(
categoryName: "Memory",
counterName: "Available MBytes"
);
}
public float GetAvailableMemory()
{
// 返回可用内存,单位为MB
return memoryCounter.NextValue();
}
}3. 磁盘读写速率监控
磁盘读写需要指定具体的磁盘实例,比如C盘实例名为C:,读取速率对应Disk Read Bytes/sec,写入速率对应Disk Write Bytes/sec:
public class DiskMonitor
{
private PerformanceCounter readCounter;
private PerformanceCounter writeCounter;
public DiskMonitor(string diskName)
{
// 初始化磁盘读取计数器,diskName格式为"C:"
readCounter = new PerformanceCounter(
categoryName: "LogicalDisk",
counterName: "Disk Read Bytes/sec",
instanceName: diskName
);
// 初始化磁盘写入计数器
writeCounter = new PerformanceCounter(
categoryName: "LogicalDisk",
counterName: "Disk Write Bytes/sec",
instanceName: diskName
);
}
public (float readSpeed, float writeSpeed) GetDiskSpeed()
{
float read = readCounter.NextValue();
float write = writeCounter.NextValue();
// 返回读写速率,单位为字节/秒
return (read, write);
}
}完整监控示例整合
下面是将上述几个监控项整合的完整示例,定时采集并打印性能指标:
class Program
{
static void Main(string[] args)
{
CpuMonitor cpuMonitor = new CpuMonitor();
MemoryMonitor memoryMonitor = new MemoryMonitor();
DiskMonitor diskMonitor = new DiskMonitor("C:");
// 等待1秒,确保计数器初始化完成
System.Threading.Thread.Sleep(1000);
while (true)
{
float cpuUsage = cpuMonitor.GetCpuUsage();
float availableMemory = memoryMonitor.GetAvailableMemory();
var (readSpeed, writeSpeed) = diskMonitor.GetDiskSpeed();
Console.WriteLine($"当前CPU使用率: {cpuUsage:F2}%");
Console.WriteLine($"当前可用内存: {availableMemory:F2} MB");
Console.WriteLine($"C盘读取速率: {readSpeed / 1024:F2} KB/s");
Console.WriteLine($"C盘写入速率: {writeSpeed / 1024:F2} KB/s");
Console.WriteLine("------------------------");
// 每3秒采集一次
System.Threading.Thread.Sleep(3000);
}
}
}使用注意事项
- 部分性能计数器需要管理员权限才能读取,运行程序时建议以管理员身份启动,避免抛出权限异常。
- 首次调用
NextValue方法时,部分计数器会返回0,需要在初始化后先调用一次,再正式采集数据。 - 不同系统的性能计数器实例名可能存在差异,比如磁盘实例名在部分系统中可能带反斜杠,需要根据实际情况调整。
- 高频采集性能数据会占用少量系统资源,建议根据实际需求设置合理的采集间隔,不要过于频繁调用。
常见性能计数器类别对照
以下是常用的性能计数器类别和对应指标,方便开发者扩展监控项:
| 类别名称 | 常用计数器名称 | 指标说明 |
|---|---|---|
| Processor | % Processor Time | CPU使用率 |
| Memory | Available MBytes | 可用物理内存 |
| Memory | % Committed Bytes In Use | 内存提交使用率 |
| LogicalDisk | % Free Space | 磁盘剩余空间占比 |
| Network Interface | Bytes Total/sec | 网络总吞吐量 |
C#性能计数器服务器性能监控PerformanceCounter修改时间:2026-05-29 04:35:34