【发布时间】:2021-02-23 12:57:13
【问题描述】:
Win2019 Server 提供了新的性能计数器,可以帮助解决带有 RDS(=远程桌面服务)的主机上的性能问题。
使用前必须使用注册表项激活它们:
https://docs.microsoft.com/en-us/windows-server/remote/remote-desktop-services/rds-rdsh-performance-counters
系统重新启动后,它们就可用了。
在 perfmon.msc 中它们是可见的并且可以使用。
但是当我开始使用 C# 代码来读取这些计数器时。
PerfCounters 在 C# 中也可见:
PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories();
foreach(var category in categories)
{
Console.WriteLine(category.CategoryName);
}
然后我开始读取性能计数器:
PerformanceCounter performanceCounter = null;
bool continueRunning = true;
try
{
performanceCounter = new PerformanceCounter("User Input Delay per Session", "Max Input Delay", "Max");
Console.WriteLine("PerfCounter created");
}
catch(Exception ex)
{
Console.WriteLine(string.Format("ERROR: Could not create PerfCounter = '{0}'", ex.Message));
continueRunning = false;
}
while (continueRunning)
{
try
{
float perfValue = performanceCounter.NextValue();
Console.WriteLine(string.Format("NextValue = '{0}'", perfValue));
}
catch(Exception ex)
{
Console.WriteLine(string.Format("ERROR: Could not read NextValue '{0}'", ex.Message));
continueRunning = false;
}
System.Threading.Thread.Sleep(2000);
}
现在,我收到程序错误和两个事件日志条目:
程序输出:
PerfCounter created
ERROR: Coud not read NextValue 'The Counter layout for the Category specified is invalid, a counter of the type: AverageCount64, AverageTimer32, CounterMultiTimer, CounterMultiTimerInverse, CounterMultiTimer100Ns, CounterMultiTimer100NsInverse, RawFraction, or SampleFraction has to be immediately followed by any of the base counter types: AverageBase, CounterMultiBase, RawBase or SampleBase.'
Program terminated
事件日志条目:
- 事件 ID 1003:“LSM”服务的可扩展计数器 DLL“C:\Windows\System32\perfts.dll”返回了不正确的对象长度。返回的 1 个对象长度的总和与返回的缓冲区大小不匹配。
- 事件 ID 1017:已禁用从“LSM”服务收集性能计数器数据,因为该服务的性能计数器库已生成一个或多个错误。强制执行此操作的错误已写入应用程序事件日志。在启用此服务的性能计数器之前更正错误。
从现在开始,性能计数器已经消失(也在 perfmon.msc 中)并且不再可见/不可用。
此外,(对我而言)它们无法重新激活。
perfmon.msc 中的性能计数器也消失了。
必须做些什么才能在 C# 中读取这些性能计数器而不会在操作系统中“丢失”它们?
也很重要
如何使丢失的性能计数器再次可见/可用?
【问题讨论】:
-
你确定这个查询在调用 NextValue() 之前不需要某种 First() 或 Seek() 吗?错误消息表明请求无效。也许 NextValue() 引用的日志行未正确初始化..
-
Microsoft 的示例仅使用 NextValue(),但我找不到 First() 方法。对我来说这有点令人沮丧,因为我无法重新激活丢失的性能计数器(重新安装操作系统不是要走的路)
标签: c# performancecounter perfmon