使用C#获取在IIS中排队的请求数

时间:2015-03-27 15:05:57

标签: c# asp.net iis azure

我需要从Azure上运行IIS-8的WebRole收集以下两个信息。

  1. 在IIS中排队的请求数
  2. 当前正在处理的请求数
  3. 由于我们使用的是Azure云服务,因此我认为最好与Azure提供的默认IIS配置结合使用。

    方法1:使用WorkerProcess请求收集

        public void EnumerateWorkerProcess()
        {
            ServerManager manager = new ServerManager();
            foreach (WorkerProcess proc in manager.WorkerProcesses)
            {
                RequestCollection req = proc.GetRequests(1000);
                Debug.WriteLine(req.Count);
            }
        }
    

    缺点:

    要求在IIS中显式启用RequestMonitor。

    方法2:使用PerformanceCounter类

        public void ReadPerformanceCounter()
        {
            var root = HostingEnvironment.MapPath("~/App_Data/PerfCount.txt");
    
            PerformanceCounter counter = new PerformanceCounter(@"ASP.NET", "requests current", true);
            float val = counter.NextValue();
            using (StreamWriter perfWriter = new StreamWriter(root, true))
            {
                perfWriter.WriteLine(val);
            }
        }
    

    缺点: 需要比当前运行IIS进程更高的权限。

    P.S。 有一个four years old SO post但没有得到很好的解答。

0 个答案:

没有答案
相关问题