방법: 카운터 및 범주 목록 검색

업데이트: 2007년 11월

PerformanceCounterCategory 클래스의 GetCounters 메서드를 사용하여 특정 범주의 모든 카운터나 인스턴스 목록을 빠르게 검색할 수 있습니다. PerformanceCounter 개체의 인스턴스를 만들어 기존 카운터로 설정한 다음 값을 검색하는 방식으로 범주에서 값을 요청할 때마다 시스템에서는 전체 범주를 읽고 요청한 카운터를 검색합니다. 예를 들어 20개의 카운터를 포함하는 범주에서 5개의 서로 다른 카운터를 요청할 경우, 시스템에서 범주에 있는 20개의 모든 카운터를 다섯 번 읽고 각 카운터를 별도로 검색합니다. 그러나 GetCounters를 사용하면 범주를 한 번만 읽고 동일한 데이터를 얻을 수 있습니다.

GetCounters 메서드는 범주의 카운터를 포함하는 PerformanceCounter 형식의 배열을 반환합니다. Item 속성을 사용하여 컬렉션의 내용을 처리할 수 있습니다.

정적 GetCategories 메서드를 사용하면 카운터를 검색할 수 있을 뿐만 아니라 현재 컴퓨터나 액세스 권한을 가진 모든 서버에서 범주 목록을 반환할 수 있습니다. GetCategoriesPerformanceCounterCategory 개체의 배열을 반환합니다.

범주에서 카운터를 검색하려면

  1. PerformanceCounter 개체를 만들고 원하는 범주를 가리키도록 구성합니다. 자세한 내용은 방법: PerformanceCounter 구성 요소 인스턴스 만들기 또는 방법: PerformanceCounter 구성 요소 인스턴스 구성을 참조하십시오.

  2. 결과 범주 목록을 포함할 PerformanceCounter 형식의 배열을 만듭니다.

  3. PerformanceCounterCategory 클래스의 GetCounters 메서드를 호출하고 원하는 범주를 매개 변수로 설정합니다.

  4. 결과를 배열로 설정합니다.

    다음 예제에서는 Cache 범주에 있는 모든 카운터를 검색하는 방법을 보여 줍니다. 이 코드에서는 단추와 ListBox 컨트롤이 있는 Windows Forms 응용 프로그램에서 작업하고 있다고 가정합니다. 또한 System.dll에 대한 참조가 있고 System.Diagnostics 네임스페이스에 대한 Imports 문(Visual Basic의 경우) 또는 using 문(C#의 경우)이 있다고 가정합니다.

    Private Sub btnGetCounters_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles btnGetCounters.Click
       Dim mypc() As PerformanceCounter
       Dim i As Integer
       Dim myCat As New PerformanceCounterCategory("Cache")
       ' Remove the current contents of the list.
       Me.ListBox1.Items.Clear()
       ' Retrieve the counters.
       mypc = myCat.GetCounters
       ' Add the retrieved counters to the list.
       For i = 0 To mypc.Length - 1
          Me.ListBox1.Items.Add(mypc(i).CounterName)
       Next
    End Sub
    
     private void btnGetCounters_Click(object sender, EventArgs e)
        {
            System.Diagnostics.PerformanceCounter[] mypc;
            System.Diagnostics.PerformanceCounterCategory mycat =
               new System.Diagnostics.PerformanceCounterCategory("cache");
            // Remove the current contents of the list.
            this.listBox1.Items.Clear();
            // Retrieve the counters.
            mypc = mycat.GetCounters();
            // Add the retrieved counters to the list.
            for (int i = 0; i < mypc.Length; i++)
            {
                this.listBox1.Items.Add(mypc[i].CounterName);
            }
        }
    

컴퓨터의 모든 범주를 검색하려면

  1. 결과 범주 목록을 포함할 PerformanceCounter 형식의 배열을 만듭니다.

  2. PerformanceCounterCategory 클래스의 GetCategories 메서드를 호출하고 원하는 범주를 매개 변수로 설정합니다.

  3. 결과를 배열로 설정합니다.

    다음 코드에서는 로컬 컴퓨터의 모든 범주를 검색하는 방법을 보여 줍니다. 이 코드에서는 단추와 ListBox 컨트롤이 있는 Windows Forms 응용 프로그램에서 작업하고 있다고 가정합니다. 또한 System.dll에 대한 참조가 있고 System.Diagnostics 네임스페이스에 대한 Imports 문(Visual Basic의 경우) 또는 using 문(C#의 경우)이 있다고 가정합니다.

    Private Sub btnGetCategories_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs)
       Dim myCat2 As PerformanceCounterCategory()
       Dim i As Integer
       ' Clear the contents of the list box.
       Me.listBox2.Items.Clear()
       ' Retrieve the categories.
       myCat2 = PerformanceCounterCategory.GetCategories
       ' Add the retrieved categories to the list.
       For i = 0 To myCat2.Length - 1
          Me.listBox2.Items.Add(myCat2(i).CategoryName)
       Next
    End Sub
    
     private void btnGetCategories_Click(object sender, EventArgs e)
        {
            System.Diagnostics.PerformanceCounterCategory[] myCat2;
            // Clear the list's current contents.
            this.listBox2.Items.Clear();
            // Retrieve the categories.
            myCat2 =
               System.Diagnostics.PerformanceCounterCategory.GetCategories();
            // Add the retrieved categories to the list.
            for (int i = 0; i < myCat2.Length; i++)
            {
                this.listBox2.Items.Add(myCat2[i].CategoryName);
            }
        }
    

참고 항목

작업

방법: PerformanceCounter 구성 요소 인스턴스 만들기

방법: PerformanceCounter 구성 요소 인스턴스 구성

개념

성능 카운터 값 검색