Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

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

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:MMTOneStudioTech.WebInfrastructureCustomMachineMonitoring.cs:line 33

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
            }
        }

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...