PerformanceCounterCategory 클래스

정의

성능 카운터의 범주를 정의하는 성능 개체를 나타냅니다.

public ref class PerformanceCounterCategory sealed
public sealed class PerformanceCounterCategory
type PerformanceCounterCategory = class
Public NotInheritable Class PerformanceCounterCategory
상속
PerformanceCounterCategory

예제

다음 코드 예제에서는 여부를 확인 합니다 PerformanceCounterPerformanceCounterCategory 로컬 컴퓨터 또는 다른 컴퓨터에 존재 합니다. 이러한 개체가 로컬 컴퓨터에 없는 경우 예제에서는 필요에 따라 개체를 만듭니다. 메서드를 Exists 사용하여 가 PerformanceCounterCategory 있는지 여부를 확인합니다. 가 PerformanceCounterCategory 없고 카운터 이름이 지정되지 않았거나 컴퓨터가 원격 컴퓨터인 경우 예제가 종료됩니다.

PerformanceCounter 이름이 제공된 경우 예제에서는 메서드를 CounterExists 사용하고 결과를 사용자에게 표시합니다. 가 PerformanceCounter 없으면 사용자는 새 PerformanceCounter를 사용하여 를 PerformanceCounterCategory 삭제하고 다시 만들 수 있습니다. 사용자가 그렇게 하면 메서드를 사용하여 범주가 Delete 삭제됩니다.

요청된 경우 이 예제는 이제 새 PerformanceCounterCategoryPerformanceCounter 를 만들고 메서드를 Create 사용합니다. instance 이름을 지정하면 이 예제에서는 메서드를 InstanceExists 사용하고 결과를 표시합니다.

using System;
using System.Diagnostics;
using Microsoft.VisualBasic;

class PerfCounterCatCreateExistMod
{

    public static void Main(string[] args)
    {
        string categoryName = "";
        string counterName = "";
        string instanceName = "";
        string machineName = "";
        string categoryHelp = "";
        string counterHelp = "";
        bool objectExists = false;
        PerformanceCounterCategory pcc;
        bool createCategory = false;

        // Copy the supplied arguments into the local variables.
        try
        {
            categoryName = args[0];
            counterName = args[1];
            instanceName = args[2];
            machineName = args[3]=="."? "": args[3];
            categoryHelp = args[4];
            counterHelp = args[5];
        }
        catch(Exception ex)
        {
            // Ignore the exception from non-supplied arguments.
        }

        // Verify that the category name is not blank.
        if (categoryName.Length==0)
        {
            Console.WriteLine("Category name cannot be blank.");
            return;
        }

        // Check whether the specified category exists.
        if (machineName.Length==0)
        {
            objectExists = PerformanceCounterCategory.Exists(categoryName);

        }
        else
        {
            // Handle the exception that is thrown if the computer
            // cannot be found.
            try
            {
                objectExists = PerformanceCounterCategory.Exists(categoryName, machineName);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error checking for existence of " +
                    "category \"{0}\" on computer \"{1}\":"+"\n" +ex.Message, categoryName, machineName);
                return;
            }
        }

        // Tell the user whether the specified category exists.
        Console.WriteLine("Category \"{0}\" "+ (objectExists? "exists on ": "does not exist on ")+
            (machineName.Length>0? "computer \"{1}\".": "this computer."), categoryName, machineName);

        // If no counter name is given, the program cannot continue.
        if (counterName.Length==0)
        {
            return;
        }

        // A category can only be created on the local computer.
        if (!objectExists)
        {
            if (machineName.Length>0)
            {
                return;
            }
            else
            {
                createCategory = true;
            }
        }
        else
        {
            // Check whether the specified counter exists.
            if (machineName.Length==0)
            {
                objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName);
            }
            else
            {
                objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName, machineName);
            }

            // Tell the user whether the counter exists.
            Console.WriteLine("Counter \"{0}\" "+(objectExists? "exists": "does not exist")+
                " in category \"{1}\" on "+(machineName.Length>0? "computer \"{2}\".": "this computer."),
                counterName, categoryName, machineName);

            // If the counter does not exist, consider creating it.
            if (!objectExists)

                // If this is a remote computer,
                // exit because the category cannot be created.
            {
                if (machineName.Length>0)
                {
                    return;
                }
                else
                {
                    // Ask whether the user wants to recreate the category.
                    Console.Write("Do you want to delete and recreate " +
                        "category \"{0}\" with your new counter? [Y/N]: ", categoryName);
                    string userReply = Console.ReadLine();

                    // If yes, delete the category so it can be recreated later.
                    if (userReply.Trim().ToUpper()=="Y")
                    {
                        PerformanceCounterCategory.Delete(categoryName);
                        createCategory = true;
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }

        // Create the category if it was deleted or it never existed.
        if (createCategory)
        {
            pcc = PerformanceCounterCategory.Create(categoryName, categoryHelp, counterName, counterHelp);

            Console.WriteLine("Category \"{0}\" with counter \"{1}\" created.", pcc.CategoryName, counterName);
        }
        else if(instanceName.Length>0)
        {
            if (machineName.Length==0)
            {
                objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName);
            }
            else
            {
                objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName, machineName);
            }

            // Tell the user whether the instance exists.
            Console.WriteLine("Instance \"{0}\" "+(objectExists? "exists": "does not exist")+
                " in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
                instanceName, categoryName, machineName);
        }
    }
}
Imports System.Diagnostics

Module PerfCounterCatCreateExistMod

    Sub Main(ByVal args() As String)
        Dim categoryName As String = ""
        Dim counterName As String = ""
        Dim instanceName As String = ""
        Dim machineName As String = ""
        Dim categoryHelp As String = ""
        Dim counterHelp As String = ""
        Dim objectExists As Boolean = False
        Dim pcc As PerformanceCounterCategory
        Dim createCategory As Boolean = False

        ' Copy the supplied arguments into the local variables.
        Try
            categoryName = args(0)
            counterName = args(1)
            instanceName = args(2)
            machineName = IIf(args(3) = ".", "", args(3))
            categoryHelp = args(4)
            counterHelp = args(5)
        Catch ex As Exception
            ' Ignore the exception from non-supplied arguments.
        End Try

        ' Verify that the category name is not blank.
        If categoryName.Length = 0 Then
            Console.WriteLine("Category name cannot be blank.")
            Return
        End If

        ' Check whether the specified category exists.
        If machineName.Length = 0 Then
            objectExists = _
                PerformanceCounterCategory.Exists(categoryName)

        Else
            ' Handle the exception that is thrown if the computer 
            ' cannot be found.
            Try
                objectExists = PerformanceCounterCategory.Exists( _
                    categoryName, machineName)
            Catch ex As Exception
                Console.WriteLine("Error checking for existence of " & _
                    "category ""{0}"" on computer ""{1}"":" & vbCrLf & _
                    ex.Message, categoryName, machineName)
                Return
            End Try
        End If

        ' Tell the user whether the specified category exists.
        Console.WriteLine("Category ""{0}"" " & _
            IIf(objectExists, "exists on ", "does not exist on ") & _
            IIf(machineName.Length > 0, _
                "computer ""{1}"".", "this computer."), _
            categoryName, machineName)

        ' If no counter name is given, the program cannot continue.
        If counterName.Length = 0 Then
            Return
        End If

        ' A category can only be created on the local computer.
        If Not objectExists Then
            If machineName.Length > 0 Then
                Return
            Else
                createCategory = True
            End If
        Else
            ' Check whether the specified counter exists.
            If machineName.Length = 0 Then
                objectExists = PerformanceCounterCategory.CounterExists( _
                    counterName, categoryName)
            Else
                objectExists = PerformanceCounterCategory.CounterExists( _
                    counterName, categoryName, machineName)
            End If

            ' Tell the user whether the counter exists.
            Console.WriteLine("Counter ""{0}"" " & _
                IIf(objectExists, "exists", "does not exist") & _
                " in category ""{1}"" on " & _
                IIf(machineName.Length > 0, _
                    "computer ""{2}"".", "this computer."), _
                counterName, categoryName, machineName)

            ' If the counter does not exist, consider creating it.
            If Not objectExists Then

                ' If this is a remote computer, 
                ' exit because the category cannot be created.
                If machineName.Length > 0 Then
                    Return
                Else
                    ' Ask whether the user wants to recreate the category.
                    Console.Write("Do you want to delete and recreate " & _
                        "category ""{0}"" with your new counter? [Y/N]: ", _
                        categoryName)
                    Dim userReply As String = Console.ReadLine()

                    ' If yes, delete the category so it can be recreated later.
                    If userReply.Trim.ToUpper.Chars(0) = "Y" Then
                        PerformanceCounterCategory.Delete(categoryName)
                        createCategory = True
                    Else
                        Return
                    End If
                End If
            End If
        End If

        ' Create the category if it was deleted or it never existed.
        If createCategory Then
            pcc = PerformanceCounterCategory.Create( _
                categoryName, categoryHelp, counterName, counterHelp)

            Console.WriteLine( _
                "Category ""{0}"" with counter ""{1}"" created.", _
                pcc.CategoryName, counterName)

        ElseIf instanceName.Length > 0 Then

            ' If an instance name was given, check whether it exists.
            If machineName.Length = 0 Then
                objectExists = PerformanceCounterCategory.InstanceExists( _
                    instanceName, categoryName)
            Else
                objectExists = PerformanceCounterCategory.InstanceExists( _
                    instanceName, categoryName, machineName)
            End If

            ' Tell the user whether the instance exists.
            Console.WriteLine("Instance ""{0}"" " & _
                IIf(objectExists, "exists", "does not exist") & _
                " in category ""{1}"" on " & _
                IIf(machineName.Length > 0, _
                    "computer ""{2}"".", "this computer."), _
                instanceName, categoryName, machineName)
        End If
    End Sub
End Module

설명

중요

성능 카운터를 만들거나 삭제하려면 명명된 뮤텍스를 사용하여 기본 코드를 동기화해야 합니다. 높은 권한이 필요한 애플리케이션을 명명 된 뮤텍스를 잠그는 경우 만들거나 성능 카운터를 삭제 하려고 하면 애플리케이션이 잠금이 해제 될 때까지 응답을 중지 합니다. 이 문제를 방지하려면 신뢰할 수 없는 코드에 대한 권한을 부여 UnmanagedCode 하지 마세요. 또한 UnmanagedCode 사용 권한은 잠재적으로 다른 권한을 바이패스할 수 있도록 허용하며 신뢰할 수 있는 코드에만 부여되어야 합니다.

합니다 PerformanceCounterCategory 인스턴스의 CategoryName 성능 개체에 대 한 분야 성능 뷰어 애플리케이션의 카운터 추가 대화 상자에에서 속성이 표시 됩니다.

클래스는 PerformanceCounterCategory 컴퓨터의 카운터 및 범주와 상호 작용하기 위한 여러 가지 메서드를 제공합니다. 메서드를 Create 사용하면 사용자 지정 범주를 정의할 수 있습니다. 메서드는 Delete 컴퓨터에서 범주를 제거하는 방법을 제공합니다. GetCategories 메서드를 사용하면 범주 목록을 볼 수 있지만 ReadCategory 모든 카운터를 검색하고 단일 범주와 연결된 데이터를 instance.

성능 카운터는 애플리케이션에 대 한 성능 데이터를 게시합니다. 범주에는 물리적 구성 요소(예: 프로세서, 디스크 및 메모리) 및 시스템 개체(예: 프로세스 및 스레드)가 포함됩니다. 동일한 성능 개체와 관련된 시스템 카운터는 공통 포커스를 나타내는 범주로 그룹화됩니다. 클래스의 PerformanceCounter instance 만들 때 먼저 구성 요소가 상호 작용할 범주를 표시한 다음 해당 범주에서 카운터를 선택합니다.

예를 들어 하나의 Windows 카운터 범주는 메모리 범주입니다. 이 범주 내의 시스템 카운터는 사용 가능한 바이트 수 및 캐시된 바이트 수와 같은 메모리 데이터를 추적합니다. 애플리케이션에서 캐시 된 바이트를 사용 하려는 경우 인스턴스의 만듭니다는 PerformanceCounter 구성 요소를 Memory 범주에 연결 하 고 해당 범주 (이 예제의 경우 캐시 된 바이트)에 적절 한 카운터를 선택 합니다.

시스템에서 더 많은 카운터 범주를 사용할 수 있지만 가장 자주 상호 작용할 범주는 캐시, 메모리, 개체, PhysicalDisk, Process, Processor, Server, System 및 Thread 범주입니다.

중요

클래스의 PerformanceCounter 메서드는 RemoveInstance 카운터를 해제하고 해당 범주에 대해 재사용 옵션을 선택하면 카운터의 instance 다시 사용됩니다. 다른 프로세스 또는 코드의 다른 부분이 카운터 instance 쓰려고 하는 경우 경합 상태가 발생할 수 있습니다.

참고

애플리케이션을 설치 하는 동안, 애플리케이션의 실행 중이 아니라 새 성능 카운터 범주를 만들 수 있음을 것이 좋습니다. 이렇게 하면 운영 체제가 등록된 성능 카운터 범주 목록을 새로 고칠 수 있습니다. 목록을 새로 고치지 않은 경우 범주를 사용하려고 하면 실패합니다.

참고

.NET Framework 2.0과 함께 설치된 성능 카운터 범주는 별도의 공유 메모리를 사용하며 각 성능 카운터 범주에는 자체 메모리가 있습니다. 레지스트리 키 <HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\category name>\Performance에 FileMappingSize라는 DWORD를 만들어 별도의 공유 메모리 크기를 지정할 수 있습니다. FileMappingSize 값 범주의 공유 메모리 크기 설정 됩니다. 기본 크기는 10 진수 131072입니다. FileMappingSize 값 없는 경우는 fileMappingSize 특성에 대 한 값을 performanceCounters 합니다 Machine.config 파일에 지정 된 요소를 사용할 구성 파일을 처리에 대 한 추가 오버 헤드를 유발 합니다. 레지스트리에서 파일 매핑 크기를 설정 하 여 애플리케이션 시작에 대 한 성능 향상을 이룰 수 있습니다. 파일 매핑 크기에 대한 자세한 내용은 performanceCounters를 참조<하세요>.

생성자

PerformanceCounterCategory()

PerformanceCounterCategory 클래스의 새 인스턴스를 초기화하고 CategoryName 속성을 비운 다음 MachineName 속성을 로컬 컴퓨터로 설정합니다.

PerformanceCounterCategory(String)

PerformanceCounterCategory 클래스의 새 인스턴스를 초기화하고 CategoryName 속성을 지정한 값으로 설정한 다음 MachineName 속성을 로컬 컴퓨터로 설정합니다.

PerformanceCounterCategory(String, String)

PerformanceCounterCategory 클래스의 새 인스턴스를 초기화하고 CategoryNameMachineName 속성을 지정한 값으로 설정합니다.

속성

CategoryHelp

범주의 도움말 텍스트를 가져옵니다.

CategoryName

이 범주를 정의하는 성능 개체의 이름을 가져오거나 설정합니다.

CategoryType

성능 카운터 범주 형식을 가져옵니다.

MachineName

이 범주가 있는 컴퓨터의 이름을 가져오거나 설정합니다.

메서드

CounterExists(String)

지정한 카운터가 CategoryNameMachineName 속성이 나타내는 이 범주에 등록되었는지 여부를 확인합니다.

CounterExists(String, String)

지정한 카운터가 로컬 컴퓨터의 지정한 범주에 등록되었는지 여부를 확인합니다.

CounterExists(String, String, String)

지정한 카운터가 원격 컴퓨터의 지정한 범주에 등록되었는지 여부를 확인합니다.

Create(String, String, CounterCreationDataCollection)
사용되지 않음.
사용되지 않음.
사용되지 않음.

지정한 카운터를 포함하는 사용자 지정 성능 카운터 범주를 로컬 컴퓨터에 등록합니다.

Create(String, String, PerformanceCounterCategoryType, CounterCreationDataCollection)

지정한 카운터를 포함하는 사용자 지정 성능 카운터 범주를 로컬 컴퓨터에 등록합니다.

Create(String, String, PerformanceCounterCategoryType, String, String)

NumberOfItems32 형식의 단일 카운터를 포함하는 사용자 지정 성능 카운터 범주를 로컬 컴퓨터에 등록합니다.

Create(String, String, String, String)
사용되지 않음.
사용되지 않음.
사용되지 않음.

NumberOfItems32 형식의 단일 카운터를 포함하는 사용자 지정 성능 카운터 범주를 로컬 컴퓨터에 등록합니다.

Delete(String)

로컬 컴퓨터에서 범주와 관련 카운터를 제거합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
Exists(String)

범주가 로컬 시스템에 등록되었는지 여부를 확인합니다.

Exists(String, String)

범주가 지정한 컴퓨터에 등록되었는지 여부를 확인합니다.

GetCategories()

로컬 컴퓨터에 등록된 성능 카운터 범주의 목록을 검색합니다.

GetCategories(String)

지정한 컴퓨터에 등록된 성능 카운터 범주의 목록을 검색합니다.

GetCounters()

정확히 하나의 인스턴스가 포함된 성능 카운터 범주에서 카운터 목록을 검색합니다.

GetCounters(String)

인스턴스가 하나 이상 포함된 성능 카운터 범주에서 카운터 목록을 검색합니다.

GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetInstanceNames()

이 범주와 관련된 성능 개체 인스턴스 목록을 검색합니다.

GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
InstanceExists(String)

지정한 성능 개체 인스턴스가 이 PerformanceCounterCategory 개체의 CategoryName 속성에 의해 식별된 범주에 있는지 여부를 확인합니다.

InstanceExists(String, String)

로컬 컴퓨터의 지정한 범주에 지정한 성능 개체 인스턴스가 있는지 여부를 확인합니다.

InstanceExists(String, String, String)

지정한 컴퓨터의 지정한 범주에 지정한 성능 개체 인스턴스가 있는지 여부를 확인합니다.

MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ReadCategory()

이 성능 카운터 범주와 관련된 카운터와 성능 개체 인스턴스를 모두 읽습니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보