我正在尝试读取 Azure 辅助角色应用程序 (C#) 中“HTTP 服务请求队列”类别中的性能计数器。
默认情况下,应用程序无法从该类别获取任何计数器,即
string performanceCategoryName = "HTTP Service Request Queues";
var httpQueueCategory = PerformanceCounterCategory.GetCategories().FirstOrDefault(category => category.CategoryName == performanceCategoryName);
给出 null。
但是,如果我远程桌面进入实例并启动 perfmon,我可以看到那里的类别。如果我从 perfmon 查看它然后重新启动应用程序,那么应用程序也可以读取计数器。就好像 perfmon 创建了计数器,但默认情况下它并不存在。
有人知道解决这个问题的方法吗?
最佳答案
我通过启动 cmd 进程并在 owin 监听器启动后从我的应用程序中调用“typeperf -q”解决了这个问题。
这与打开 perfmon 具有相同的效果,即默认情况下不存在的类别现在可供我的应用程序使用。 确保在尝试访问计数器类别之前等待 typeperf 列表完成(这在小型 azure 辅助角色实例上大约需要 20 秒)。
这对我有用:
var startInfo = new ProcessStartInfo()
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C typeperf.exe -q",
};
Process p = new Process() { StartInfo = startInfo };
p.Start();
p.WaitForExit(2*60*1000);
string performanceCategoryName = "HTTP Service Request Queues";
var httpQueueCategory = PerformanceCounterCategory.GetCategories().FirstOrDefault(category => category.CategoryName == performanceCategoryName);
请注意,在注册应用程序 http 监听器后进行列表很重要。否则您的实例将不可用。我注意到这一点是因为我尝试在辅助角色启动之前将 cmd 作为启动任务运行,但这不起作用。
关于c# - C# 中无法使用 Windows 性能计数器(在 Azure 辅助角色上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44500742/