PerformanceCounterCategory.Create 方法

定義

使用系統登錄自訂效能計數器分類和一個或多個計數器。

多載

Create(String, String, String, String)
已淘汰.
已淘汰.
已淘汰.

在本機電腦上登錄包含 NumberOfItems32 型別單一計數器的自訂效能計數器分類。

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

在本機電腦上登錄包含 NumberOfItems32 型別之單一計數器的自訂效能計數器分類。

Create(String, String, CounterCreationDataCollection)
已淘汰.
已淘汰.
已淘汰.

在本機電腦上登錄包含指定計數器的自訂效能計數器分類。

Create(String, String, PerformanceCounterCategoryType, CounterCreationDataCollection)

在本機電腦上登錄包含指定計數器的自訂效能計數器分類。

Create(String, String, String, String)

來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs

警告

This method has been deprecated. Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, string counterName, string counterHelp) instead. http://go.microsoft.com/fwlink/?linkid=14202

警告

This method has been deprecated. Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, string counterName, string counterHelp) instead. https://go.microsoft.com/fwlink/?linkid=14202

警告

This overload of PerformanceCounterCategory.Create has been deprecated. Use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, string counterName, string counterHelp) instead.

在本機電腦上登錄包含 NumberOfItems32 型別單一計數器的自訂效能計數器分類。

public:
 static System::Diagnostics::PerformanceCounterCategory ^ Create(System::String ^ categoryName, System::String ^ categoryHelp, System::String ^ counterName, System::String ^ counterHelp);
[System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, string counterName, string counterHelp) instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
public static System.Diagnostics.PerformanceCounterCategory Create (string categoryName, string categoryHelp, string counterName, string counterHelp);
[System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, string counterName, string counterHelp) instead.  https://go.microsoft.com/fwlink/?linkid=14202")]
public static System.Diagnostics.PerformanceCounterCategory Create (string categoryName, string categoryHelp, string counterName, string counterHelp);
[System.Obsolete("This overload of PerformanceCounterCategory.Create has been deprecated. Use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, string counterName, string counterHelp) instead.")]
public static System.Diagnostics.PerformanceCounterCategory Create (string categoryName, string categoryHelp, string counterName, string counterHelp);
public static System.Diagnostics.PerformanceCounterCategory Create (string categoryName, string categoryHelp, string counterName, string counterHelp);
[<System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, string counterName, string counterHelp) instead.  http://go.microsoft.com/fwlink/?linkid=14202")>]
static member Create : string * string * string * string -> System.Diagnostics.PerformanceCounterCategory
[<System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, string counterName, string counterHelp) instead.  https://go.microsoft.com/fwlink/?linkid=14202")>]
static member Create : string * string * string * string -> System.Diagnostics.PerformanceCounterCategory
[<System.Obsolete("This overload of PerformanceCounterCategory.Create has been deprecated. Use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, string counterName, string counterHelp) instead.")>]
static member Create : string * string * string * string -> System.Diagnostics.PerformanceCounterCategory
static member Create : string * string * string * string -> System.Diagnostics.PerformanceCounterCategory
Public Shared Function Create (categoryName As String, categoryHelp As String, counterName As String, counterHelp As String) As PerformanceCounterCategory

參數

categoryName
String

要建立並登錄於系統的自訂效能計數器分類名稱。

categoryHelp
String

自訂分類的描述。

counterName
String

屬於 NumberOfItems32 型別的新計數器的名稱,要建立為新分類的部分。

counterHelp
String

與新的自訂分類相關聯的計數器描述。

傳回

PerformanceCounterCategory,與新的系統分類或效能物件相關聯。

屬性

例外狀況

counterNamenull 或空字串 ("")。

-或-

counterName 所指定的計數器已存在。

-或-

counterName 含無效語法。 它可能包含反斜杠字元 (“\”) 或長度大於 80 個字元。

分類已存在於本機電腦上。

categoryNamenull

-或-

counterHelpnull

對基礎系統 API 的呼叫失敗。

以不具有系統管理員權限執行的程式碼嘗試讀取效能計數器。

範例

下列程式代碼範例會使用 Create(String, String, String, String) 方法,為每個建立一個和單PerformanceCounterPerformanceCounterCategory的說明文字。

public static void Main(string[] args)
{
    string categoryName = "";
    string counterName = "";
    string categoryHelp = "";
    string counterHelp = "";
    PerformanceCounterCategory pcc;

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

    Console.WriteLine("Category name: \"{0}\"", categoryName);
    Console.WriteLine("Category help: \"{0}\"", categoryHelp);
    Console.WriteLine("Counter name:  \"{0}\"", counterName);
    Console.WriteLine("Counter help:  \"{0}\"", counterHelp);

    // Use the Create overload that creates a single counter.
    try
    {
        pcc = PerformanceCounterCategory.Create(categoryName, categoryHelp, counterName, counterHelp);
        Console.WriteLine("Category \"{0}\" created.", pcc.CategoryName);
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to create the above category and counter:" + "\n" + ex.Message);
    }
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim counterName As String = ""
    Dim categoryHelp As String = ""
    Dim counterHelp As String = ""
    Dim pcc As PerformanceCounterCategory

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

    Console.WriteLine("Category name: ""{0}""", categoryName)
    Console.WriteLine("Category help: ""{0}""", categoryHelp)
    Console.WriteLine("Counter name:  ""{0}""", counterName)
    Console.WriteLine("Counter help:  ""{0}""", counterHelp)

    ' Use the Create overload that creates a single counter.
    Try
        pcc = PerformanceCounterCategory.Create( _
            categoryName, categoryHelp, counterName, counterHelp)
        Console.WriteLine("Category ""{0}"" created.", pcc.CategoryName)

    Catch ex As Exception
        Console.WriteLine( _
            "Unable to create the above category and counter:" & _
            vbCrLf & ex.Message)
    End Try
End Sub

備註

注意

若要從 Windows Vista 和更新版本中的非互動式登錄會話讀取性能計數器、Windows XP Professional x64 Edition 或 Windows Server 2003,您必須是 效能監視器 Users 群組的成員,或具有系統管理許可權。

若要避免提高許可權以存取 Windows Vista 和更新版本中的性能計數器,請將您自己新增至 效能監視器 Users 群組。

在 Windows Vista (含) 以後版本中,使用者帳戶控制 (UAC) 會判斷使用者的權限。 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。 根據預設,您會屬於標準使用者角色。 若要執行存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 您可以在啟動應用程式時,以滑鼠右鍵按一下應用程式圖示,並指出您想要以系統管理員身分執行,藉此提高為系統管理員權限。

另請參閱

適用於

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

來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs

在本機電腦上登錄包含 NumberOfItems32 型別之單一計數器的自訂效能計數器分類。

public:
 static System::Diagnostics::PerformanceCounterCategory ^ Create(System::String ^ categoryName, System::String ^ categoryHelp, System::Diagnostics::PerformanceCounterCategoryType categoryType, System::String ^ counterName, System::String ^ counterHelp);
public static System.Diagnostics.PerformanceCounterCategory Create (string categoryName, string categoryHelp, System.Diagnostics.PerformanceCounterCategoryType categoryType, string counterName, string counterHelp);
static member Create : string * string * System.Diagnostics.PerformanceCounterCategoryType * string * string -> System.Diagnostics.PerformanceCounterCategory
Public Shared Function Create (categoryName As String, categoryHelp As String, categoryType As PerformanceCounterCategoryType, counterName As String, counterHelp As String) As PerformanceCounterCategory

參數

categoryName
String

要建立並登錄於系統的自訂效能計數器分類名稱。

categoryHelp
String

自訂分類的描述。

categoryType
PerformanceCounterCategoryType

指定分類是 MultiInstanceSingleInstanceUnknown 的其中一個 PerformanceCounterCategoryType 值。

counterName
String

要建立為新分類一部分的新計數器名稱。

counterHelp
String

與新的自訂分類相關聯的計數器描述。

傳回

PerformanceCounterCategory,與新的系統分類或效能物件相關聯。

例外狀況

counterNamenull 或空字串 ("")。

-或-

counterName 所指定的計數器已存在。

-或-

counterName 含無效語法。 它可能包含反斜杠字元 (“\”) 或長度大於 80 個字元。

分類已存在於本機電腦上。

categoryNamenull

-或-

counterHelpnull

對基礎系統 API 的呼叫失敗。

以不具有系統管理員權限執行的程式碼嘗試讀取效能計數器。

範例

下列程式代碼範例示範 如何使用 Create 方法。 此範例示範如何建立自定義的單一實例性能計數器類別。

Console.WriteLine("Creating Inventory custom counter");
if (!PerformanceCounterCategory.Exists("Inventory"))
    PerformanceCounterCategory.Create("Inventory",
        "Truck inventory",
        PerformanceCounterCategoryType.SingleInstance,
        "Trucks", "Number of trucks on hand");
Console.WriteLine("Creating Inventory custom counter")
If Not PerformanceCounterCategory.Exists("Inventory") Then
    PerformanceCounterCategory.Create("Inventory", "Truck inventory", PerformanceCounterCategoryType.SingleInstance, "Trucks", "Number of trucks on hand")
End If

備註

參數 categoryType 會指定性能計數器類別是單一實例還是多重實例。 根據預設,類別會在建立時為單一實例,並在新增另一個實例時變成多重實例。 設定應用程式時會建立類別,並在運行時間新增實例。 在 .NET Framework 1.0 和 1.1 版中,不需要知道性能計數器類別是否為多重實例或單一實例。 在 .NET Framework 2.0 中PerformanceCounterCategoryType,列舉是用來指出性能計數器是否可以有多個實例。

隨 .NET Framework 2.0 一起安裝的性能計數器類別會使用不同的共用記憶體,而每個性能計數器類別都有自己的記憶體。 您可以在登錄機碼中建立名為 FileMappingSize 的 DWORD,以指定個別共用記憶體的大小,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<類別名稱>\效能。 FileMappingSize 值會設定為類別的共用記憶體大小。 默認大小為十進位131072。 如果 FileMappingSize 值不存在,則會 fileMappingSize 使用 Machine.config 檔案中指定的元素屬性值 performanceCounters ,因而造成組態檔處理的額外負荷。 您可以藉由在登錄中設定檔案對應大小,來實現應用程式啟動的效能改善。

注意

強烈建議在安裝應用程式期間建立新的性能計數器類別,而不是在執行應用程式期間。 這可讓操作系統重新整理其已註冊性能計數器類別清單的時間。 如果清單尚未重新整理,則嘗試使用類別將會失敗。

注意

若要從 Windows Vista 和更新版本中的非互動式登錄會話讀取性能計數器、Windows XP Professional x64 Edition 或 Windows Server 2003,您必須是 效能監視器 Users 群組的成員,或具有系統管理許可權。

若要避免提高許可權以存取 Windows Vista 和更新版本中的性能計數器,請將您自己新增至 效能監視器 Users 群組。

在 Windows Vista (含) 以後版本中,使用者帳戶控制 (UAC) 會判斷使用者的權限。 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。 根據預設,您會屬於標準使用者角色。 若要執行存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 您可以在啟動應用程式時,以滑鼠右鍵按一下應用程式圖示,並指出您想要以系統管理員身分執行,藉此提高為系統管理員權限。

另請參閱

適用於

Create(String, String, CounterCreationDataCollection)

來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs

警告

This method has been deprecated. Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData) instead. http://go.microsoft.com/fwlink/?linkid=14202

警告

This method has been deprecated. Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData) instead. https://go.microsoft.com/fwlink/?linkid=14202

警告

This overload of PerformanceCounterCategory.Create has been deprecated. Use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData) instead.

在本機電腦上登錄包含指定計數器的自訂效能計數器分類。

public:
 static System::Diagnostics::PerformanceCounterCategory ^ Create(System::String ^ categoryName, System::String ^ categoryHelp, System::Diagnostics::CounterCreationDataCollection ^ counterData);
[System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData) instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
public static System.Diagnostics.PerformanceCounterCategory Create (string categoryName, string categoryHelp, System.Diagnostics.CounterCreationDataCollection counterData);
[System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData) instead.  https://go.microsoft.com/fwlink/?linkid=14202")]
public static System.Diagnostics.PerformanceCounterCategory Create (string categoryName, string categoryHelp, System.Diagnostics.CounterCreationDataCollection counterData);
[System.Obsolete("This overload of PerformanceCounterCategory.Create has been deprecated. Use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData) instead.")]
public static System.Diagnostics.PerformanceCounterCategory Create (string categoryName, string categoryHelp, System.Diagnostics.CounterCreationDataCollection counterData);
public static System.Diagnostics.PerformanceCounterCategory Create (string categoryName, string categoryHelp, System.Diagnostics.CounterCreationDataCollection counterData);
[<System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData) instead.  http://go.microsoft.com/fwlink/?linkid=14202")>]
static member Create : string * string * System.Diagnostics.CounterCreationDataCollection -> System.Diagnostics.PerformanceCounterCategory
[<System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData) instead.  https://go.microsoft.com/fwlink/?linkid=14202")>]
static member Create : string * string * System.Diagnostics.CounterCreationDataCollection -> System.Diagnostics.PerformanceCounterCategory
[<System.Obsolete("This overload of PerformanceCounterCategory.Create has been deprecated. Use System.Diagnostics.PerformanceCounterCategory.Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData) instead.")>]
static member Create : string * string * System.Diagnostics.CounterCreationDataCollection -> System.Diagnostics.PerformanceCounterCategory
static member Create : string * string * System.Diagnostics.CounterCreationDataCollection -> System.Diagnostics.PerformanceCounterCategory
Public Shared Function Create (categoryName As String, categoryHelp As String, counterData As CounterCreationDataCollection) As PerformanceCounterCategory

參數

categoryName
String

要建立並登錄於系統的自訂效能計數器分類名稱。

categoryHelp
String

自訂分類的描述。

counterData
CounterCreationDataCollection

CounterCreationDataCollection,指定要建立為新分類部分的計數器。

傳回

PerformanceCounterCategory,與新的自訂分類或效能物件相關聯。

屬性

例外狀況

counterData 集合中所指定的計數器名稱為 null 或空字串 ("")。

-或-

counterData 集合中所指定的計數器已存在。

-或-

counterName 參數的語法無效。 它可能包含反斜杠字元 (“\”) 或長度大於 80 個字元。

categoryName 參數為 null

分類已存在於本機電腦上。

-或-

counterData 集合的配置對基底計數器而言不正確。 AverageCount64AverageTimer32CounterMultiTimerCounterMultiTimerInverseCounterMultiTimer100NsCounterMultiTimer100NsInverseRawFractionSampleFractionSampleCounter 類型的計數器後面必須緊接著其中一個基底計數器類型 (AverageBaseMultiBaseRawBaseSampleBase)。

對基礎系統 API 的呼叫失敗。

以不具有系統管理員權限執行的程式碼嘗試讀取效能計數器。

範例

下列程式代碼範例會判斷名為 「orders」 的物件是否存在 PerformanceCounterCategory 。 如果沒有,它會使用包含兩個CounterCreationDataCollection性能計數器的物件來建立 PerformanceCounterCategory 物件。

if (  !PerformanceCounterCategory::Exists( "Orders" ) )
{
   CounterCreationData^ milk = gcnew CounterCreationData;
   milk->CounterName = "milk";
   milk->CounterType = PerformanceCounterType::NumberOfItems32;

   CounterCreationData^ milkPerSecond = gcnew CounterCreationData;
   milkPerSecond->CounterName = "milk orders/second";
   milkPerSecond->CounterType = PerformanceCounterType::RateOfCountsPerSecond32;

   CounterCreationDataCollection^ ccds = gcnew CounterCreationDataCollection;
   ccds->Add( milkPerSecond );
   ccds->Add( milk );
   PerformanceCounterCategory::Create( "Orders", "Number of processed orders", ccds );
}
if (!PerformanceCounterCategory.Exists("Orders"))
{
    CounterCreationData milk = new CounterCreationData();
    milk.CounterName = "milk";
    milk.CounterType = PerformanceCounterType.NumberOfItems32;

    CounterCreationData milkPerSecond = new CounterCreationData();
    milkPerSecond.CounterName = "milk orders/second";
    milkPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;

    CounterCreationDataCollection ccds = new CounterCreationDataCollection();
    ccds.Add(milkPerSecond);
    ccds.Add(milk);

    PerformanceCounterCategory.Create("Orders", "Number of processed orders",
        PerformanceCounterCategoryType.SingleInstance, ccds);
}
If Not PerformanceCounterCategory.Exists("Orders") Then        
    Dim milk As New CounterCreationData()
    milk.CounterName = "milk"
    milk.CounterType = PerformanceCounterType.NumberOfItems32
    
    Dim milkPerSecond As New CounterCreationData()
    milkPerSecond.CounterName = "milk orders/second"
    milkPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32
    
    Dim ccds As New CounterCreationDataCollection()
    ccds.Add(milkPerSecond)
    ccds.Add(milk)
    
    PerformanceCounterCategory.Create("Orders", "Number of processed orders", _
           PerformanceCounterCategoryType.SingleInstance, ccds)
End If

備註

注意

若要從 Windows Vista 和更新版本中的非互動式登錄會話讀取性能計數器、Windows XP Professional x64 Edition 或 Windows Server 2003,您必須是 效能監視器 Users 群組的成員,或具有系統管理許可權。

若要避免提高許可權以存取 Windows Vista 和更新版本中的性能計數器,請將您自己新增至 效能監視器 Users 群組。

在 Windows Vista (含) 以後版本中,使用者帳戶控制 (UAC) 會判斷使用者的權限。 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。 根據預設,您會屬於標準使用者角色。 若要執行存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 您可以在啟動應用程式時,以滑鼠右鍵按一下應用程式圖示,並指出您想要以系統管理員身分執行,藉此提高為系統管理員權限。

另請參閱

適用於

Create(String, String, PerformanceCounterCategoryType, CounterCreationDataCollection)

來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs

在本機電腦上登錄包含指定計數器的自訂效能計數器分類。

public:
 static System::Diagnostics::PerformanceCounterCategory ^ Create(System::String ^ categoryName, System::String ^ categoryHelp, System::Diagnostics::PerformanceCounterCategoryType categoryType, System::Diagnostics::CounterCreationDataCollection ^ counterData);
public static System.Diagnostics.PerformanceCounterCategory Create (string categoryName, string categoryHelp, System.Diagnostics.PerformanceCounterCategoryType categoryType, System.Diagnostics.CounterCreationDataCollection counterData);
static member Create : string * string * System.Diagnostics.PerformanceCounterCategoryType * System.Diagnostics.CounterCreationDataCollection -> System.Diagnostics.PerformanceCounterCategory
Public Shared Function Create (categoryName As String, categoryHelp As String, categoryType As PerformanceCounterCategoryType, counterData As CounterCreationDataCollection) As PerformanceCounterCategory

參數

categoryName
String

要建立並登錄於系統的自訂效能計數器分類名稱。

categoryHelp
String

自訂分類的描述。

counterData
CounterCreationDataCollection

CounterCreationDataCollection,指定要建立為新分類部分的計數器。

傳回

PerformanceCounterCategory,與新的自訂分類或效能物件相關聯。

例外狀況

counterData 集合中所指定的計數器名稱為 null 或空字串 ("")。

-或-

counterData 集合中所指定的計數器已存在。

-或-

counterName 含無效語法。 它可能包含反斜杠字元 (“\”) 或長度大於 80 個字元。

categoryNamenull

-或-

counterDatanull

categoryType 值超出下列值範圍:MultiInstanceSingleInstanceUnknown

分類已存在於本機電腦上。

-或-

counterData 集合的配置對基底計數器而言不正確。 AverageCount64AverageTimer32CounterMultiTimerCounterMultiTimerInverseCounterMultiTimer100NsCounterMultiTimer100NsInverseRawFractionSampleFractionSampleCounter 類型的計數器後面必須緊接著其中一個基底計數器類型 (AverageBaseMultiBaseRawBaseSampleBase)。

對基礎系統 API 的呼叫失敗。

以不具有系統管理員權限執行的程式碼嘗試讀取效能計數器。

範例

下列程式代碼範例示範 如何使用 Create 方法。 此範例示範如何針對每秒訂單數目建立自定義的多實例性能計數器類別。

CounterCreationData data1 = new CounterCreationData("Trucks",
    "Number of orders", PerformanceCounterType.NumberOfItems32);
CounterCreationData data2 = new CounterCreationData("Rate of sales",
    "Orders/second", PerformanceCounterType.RateOfCountsPerSecond32);
CounterCreationDataCollection ccds = new CounterCreationDataCollection();
ccds.Add(data1);
ccds.Add(data2);
Console.WriteLine("Creating Orders custom counter.");
if (!PerformanceCounterCategory.Exists("Orders"))
    PerformanceCounterCategory.Create("Orders",
        "Processed orders",
        PerformanceCounterCategoryType.MultiInstance,
        ccds);
Dim data1 As New CounterCreationData("Trucks", "Number of orders", PerformanceCounterType.NumberOfItems32)
Dim data2 As New CounterCreationData("Rate of sales", "Orders/second", PerformanceCounterType.RateOfCountsPerSecond32)
Dim ccds As New CounterCreationDataCollection()
ccds.Add(data1)
ccds.Add(data2)
Console.WriteLine("Creating Orders custom counter.")
If Not PerformanceCounterCategory.Exists("Orders") Then
    PerformanceCounterCategory.Create("Orders", "Processed orders", PerformanceCounterCategoryType.MultiInstance, ccds)
End If

備註

參數 categoryType 會指定性能計數器類別是單一實例還是多重實例。 根據預設,類別會在建立時為單一實例,並在新增另一個實例時變成多重實例。 設定應用程式時會建立類別,並在運行時間新增實例。 在 .NET Framework 1.0 和 1.1 版中,不需要知道性能計數器類別是否為多重實例或單一實例。 在 .NET Framework 2.0 中PerformanceCounterCategoryType,列舉是用來指出性能計數器是否可以有多個實例。

隨 .NET Framework 2.0 一起安裝的性能計數器類別會使用個別的共用記憶體,而每個性能計數器類別都有自己的記憶體。 您可以在登錄機碼中建立名為 FileMappingSize 的 DWORD,以指定個別共用記憶體的大小,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<類別名稱>\效能。 FileMappingSize 值會設定為類別的共用記憶體大小。 默認大小為十進位131072。 如果 FileMappingSize 值不存在,則會 fileMappingSize 使用 Machine.config 檔案中指定的元素屬性值 performanceCounters ,因而造成組態檔處理的額外負荷。 您可以藉由在登錄中設定檔案對應大小,來實現應用程式啟動的效能改善。

注意

強烈建議在安裝應用程式期間建立新的性能計數器類別,而不是在執行應用程式期間。 這可讓操作系統重新整理其已註冊性能計數器類別清單的時間。 如果清單尚未重新整理,則嘗試使用類別將會失敗。

注意

若要從 Windows Vista 和更新版本中的非互動式登錄會話讀取性能計數器、Windows XP Professional x64 Edition 或 Windows Server 2003,您必須是 效能監視器 Users 群組的成員,或具有系統管理許可權。

若要避免提高許可權以存取 Windows Vista 和更新版本中的性能計數器,請將您自己新增至 效能監視器 Users 群組。

在 Windows Vista (含) 以後版本中,使用者帳戶控制 (UAC) 會判斷使用者的權限。 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。 根據預設,您會屬於標準使用者角色。 若要執行存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 您可以在啟動應用程式時,以滑鼠右鍵按一下應用程式圖示,並指出您想要以系統管理員身分執行,藉此提高為系統管理員權限。

另請參閱

適用於