[C#.NET] Windows performance counter 값 가져오기

2023. 2. 3. 19:45이것저것

728x90

 

 

Reading the value of a performance counter on Windows with C# .NET

In this post we saw how to list all performance categories and the performance counters within each category. It’s equally straightforward to read the value of a performance counter. You̵…

dotnetcodr.com

 

앞 게시물에서는 모든 성능 범주(performance category)와 각 범주 내의 performance counter를 나열하는 방법을 살펴보았습니다.

performance counter의 값을 읽는 것도 똑같이 간단합니다. 최소한 performance counter의 범주(category)와 이름이 필요합니다. 카운터가 여러 인스턴스에서 사용 가능한 경우 인스턴스 이름도 지정해야 합니다.

다음 코드는 CPU 사용량 및 메모리 사용량 카운터를 읽어들일 것입니다:

private static void ReadValuesOfPerformanceCounters()
{
    PerformanceCounter processorTimeCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
    PerformanceCounter memoryUsage = new PerformanceCounter("Memory", "Available MBytes");          
    Console.WriteLine("CPU usage counter: ");
    Console.WriteLine("Category: {0}", processorTimeCounter.CategoryName);
    Console.WriteLine("Instance: {0}", processorTimeCounter.InstanceName);
    Console.WriteLine("Counter name: {0}", processorTimeCounter.CounterName);
    Console.WriteLine("Help text: {0}", processorTimeCounter.CounterHelp);
    Console.WriteLine("------------------------------");
    Console.WriteLine("Memory usage counter: ");
    Console.WriteLine("Category: {0}", memoryUsage.CategoryName);
    Console.WriteLine("Counter name: {0}", memoryUsage.CounterName);
    Console.WriteLine("Help text: {0}", memoryUsage.CounterHelp);
    Console.WriteLine("------------------------------");
    while (true)
    {
        Console.WriteLine("CPU value: {0}", processorTimeCounter.NextValue());
        Console.WriteLine("Memory value: {0}", memoryUsage.NextValue());
        Thread.Sleep(2000);
        }
}
 
 

다음은 출력의 일부입니다:

 

여기에서 진단과 관련된 모든 게시물을 볼 수 있습니다.

 
728x90