尝试获取特定进程的处理器时间的性能时,System.Diagnostics.PerformanceCounter在Azure函数中导致502错误结果

问题描述

函数发布到Azure后遇到502,在本地一切正常,但是在蔚蓝的环境中,我遇到了这个烦人的错误

502-Web服务器在充当网关或代理服务器时收到无效响应。 您要查找的页面有问题,无法显示。当Web服务器(充当网关或代理)联系上游内容服务器时,收到来自内容服务器的无效响应。

我试图找出问题所在,并注释掉引用System.Diagnostics.PerformanceCounter库的代码(3行代码,在下面的代码中)时,不再重现该错误。新线程从Azure函数调用方法,以便在主线程执行其逻辑时计算cpu使用率:

public static void MeasurecpuPerformance(ref cpuMeasurementState state)
    {
        //PerformanceCounter thecpuCounter = new PerformanceCounter("Process","% Processor Time",Process.GetCurrentProcess().ProcessName);
        //thecpuCounter.NextValue();
        state = cpuMeasurementState.Work;
        float sum = 0;
        int loops = 0;
        while (state == cpuMeasurementState.Work)
        {
            //sum += thecpuCounter.NextValue() / Environment.ProcessorCount;
            loops++;
            Thread.Sleep(1000);
        }

        float usage = sum / loops;
        Console.WriteLine("cpu Usage: {0}",usage);
        Console.WriteLine("--------------------------------------------------------");
        state = cpuMeasurementState.Sleep;
    }

任何人都知道我该怎么做才能使它起作用?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)