Azure - How can I read cpu and memory of my web app?

*爱你&永不变心* 提交于 2019-12-13 10:57:28

问题


I'm trying to read the CPU and Memory usage for my app using PerformanceCounters. code:

PerformanceCounter cpuCounter;

cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";

var result = cpuCounter.NextValue();//ERROR HERE

I'm getting a Unauthorized exception. How can I work around this?

Edit 1:
I tried to set the current instance name for both the processor count and the memory without luck...

Edit 2:
the exception .ToString() is

System.UnauthorizedAccessException: Access to the registry key 'Global' is denied. at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str) at Microsoft.Win32.RegistryKey.InternalGetValue(String name, Object defaultValue, Boolean doNotExpand, Boolean checkSecurity) at Microsoft.Win32.RegistryKey.GetValue(String name) at System.Diagnostics.PerformanceMonitor.GetData(String item) at System.Diagnostics.PerformanceCounterLib.GetPerformanceData(String item) at System.Diagnostics.PerformanceCounterLib.get_CategoryTable() at System.Diagnostics.PerformanceCounterLib.CounterExists(String category, String counter, Boolean& categoryExists) at System.Diagnostics.PerformanceCounterLib.CounterExists(String machine, String category, String counter) at System.Diagnostics.PerformanceCounter.InitializeImpl() at System.Diagnostics.PerformanceCounter.Initialize() at System.Diagnostics.PerformanceCounter.NextSample() at System.Diagnostics.PerformanceCounter.NextValue() at StudioTech.Web.Infrastructure.CustomMachineMonitoring.<>c__DisplayClass0_0.<b__0>d.MoveNext() in C:\MMT\One\StudioTech.Web\Infrastructure\CustomMachineMonitoring.cs:line 33


回答1:


According to the exception information, it indicates that we have no access to Performance Monitor. As WebApp is a sandbox, if we use the Azure WebApp, we have no access to do that.

The user account must either be a member of the Administrators group or a member of the Performance Monitor Users group in Windows.

My suggestion is that we could use Application Insight to do that. We need to configurate Application Insight for WebApp, more details please refer to the document. About Performance Counters in the Application Insight, we could refer to this tutorials.

If we try to use Application Insight API, we need to create a Apikey. We also could get demo code from the document. It works correctly for me.

  static void Main(string[] args)
        {
            var applicationId = "xxxxxxxx";
            var applicationKey = "xxxxxxxx";
            var queryPath = "performanceCounters/processCpuPercentage";
            var queryType = "metrics";
            var str = GetTelemetry(applicationId, applicationKey, queryType, queryPath, "");


        }

 public static string GetTelemetry(string appid, string apikey,
            string queryType, string queryPath, string parameterString)
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("x-api-key", apikey);
            var req = string.Format(Url, appid, queryType, queryPath, parameterString);
            HttpResponseMessage response = client.GetAsync(req).Result;
            if (response.IsSuccessStatusCode)
            {
                return response.Content.ReadAsStringAsync().Result;
            }
            else
            {
                return response.ReasonPhrase;
            }
        }



来源:https://stackoverflow.com/questions/45438843/azure-how-can-i-read-cpu-and-memory-of-my-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!