PerformanceCounterCategory 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示性能对象,它定义性能计数器的类别。
public ref class PerformanceCounterCategory sealed
public sealed class PerformanceCounterCategory
type PerformanceCounterCategory = class
Public NotInheritable Class PerformanceCounterCategory
- 继承
-
PerformanceCounterCategory
示例
下面的代码示例确定 及其PerformanceCounterCategory是存在于本地计算机还是PerformanceCounter另一台计算机上。 如果本地计算机上不存在这些对象,该示例可以选择创建它们。 它使用 Exists 方法确定 是否存在 PerformanceCounterCategory 。 PerformanceCounterCategory如果 不存在且未指定计数器名称,或者计算机是远程计算机,则退出示例。
如果提供了名称 PerformanceCounter ,则示例使用 CounterExists 方法并向用户显示结果。 PerformanceCounter如果 不存在,则用户可以删除并使用新的 PerformanceCounter重新创建 PerformanceCounterCategory 。 如果用户这样做,则使用 Delete 方法删除类别。
如果已请求,该示例现在使用 Create 方法创建新的 PerformanceCounterCategory 和 PerformanceCounter 。 如果指定了实例名称,该示例将使用 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
注解
重要
创建或删除性能计数器需要使用命名 mutex 同步基础代码。 如果高特权应用程序锁定了命名的互斥体,则尝试创建或删除性能计数器会导致应用程序停止响应,直到释放锁。 为帮助避免此问题,切勿向不受信任的代码授予 UnmanagedCode 权限。 此外, UnmanagedCode 权限可能允许绕过其他权限,并且只应授予高度受信任的代码。
实例 PerformanceCounterCategory 的 CategoryName 属性显示在性能查看器应用程序的“添加计数器”对话框的“性能对象”字段中。
类 PerformanceCounterCategory 提供了几种方法,用于与计算机上的计数器和类别进行交互。 使用 Create 方法可以定义自定义类别。 方法 Delete 提供了一种从计算机中删除类别的方法。 使用 GetCategories 方法可以查看类别列表,同时 ReadCategory 检索与单个类别关联的所有计数器和实例数据。
性能计数器发布有关应用程序的性能数据。 类别包括物理组件 ((如处理器、磁盘和内存) )和系统对象 ((如进程和线程) )。 与同一性能对象相关的系统计数器将分组到一个类别中,指示其共同焦点。 创建 类的 PerformanceCounter 实例时,首先指示组件将与之交互的类别,然后从该类别中选择计数器。
例如,一个 Windows 计数器类别是内存类别。 此类别中的系统计数器跟踪内存数据,例如可用字节数和缓存的字节数。 如果要处理应用程序中缓存的字节,可以创建组件的实例 PerformanceCounter ,将其连接到“内存”类别,然后选取相应的计数器 (在本例中,缓存字节) 该类别。
尽管你的系统提供了更多可用的计数器类别,但你可能最常与之交互的类别包括缓存、内存、对象、物理磁盘、进程、处理器、服务器、系统和线程类别。
重要
RemoveInstance类中的 PerformanceCounter 方法将释放计数器,如果为该类别选择了重用选项,则将重用计数器的实例。 如果另一个进程甚至代码的另一部分尝试写入计数器实例,这可能会导致争用条件。
注意
强烈建议在安装应用程序期间创建新的性能计数器类别,而不是在执行应用程序期间。 这允许操作系统有时间刷新其已注册的性能计数器类别列表。 如果尚未刷新列表,则尝试使用类别将失败。
注意
随 .NET Framework 2.0 一起安装的性能计数器类别使用单独的共享内存,每个性能计数器类别都有自己的内存。 可以通过在注册表项中创建名为 FileMappingSize 的 DWORD 来指定单独的共享内存的大小,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<类别名称>\Performance。 FileMappingSize 值设置为类别的共享内存大小。 默认大小为十进制131072。 如果 FileMappingSize 值不存在,则 fileMappingSize
使用 Machine.config 文件中指定的元素的属性值 performanceCounters
,从而导致配置文件处理产生额外的开销。 通过在注册表中设置文件映射大小,可以实现应用程序启动的性能改进。 有关文件映射大小的详细信息,请参阅 <performanceCounters>。
构造函数
PerformanceCounterCategory() |
初始化 PerformanceCounterCategory 类的新实例,让 CategoryName 属性保持为空,并将 MachineName 属性设置为本地计算机。 |
PerformanceCounterCategory(String) |
初始化 PerformanceCounterCategory 类的新实例,将 CategoryName 属性设置为指定的值,并将 MachineName 属性设置为本地计算机。 |
PerformanceCounterCategory(String, String) |
初始化 PerformanceCounterCategory 类的新实例,并将 CategoryName 和 MachineName 属性设置为指定的值。 |
属性
CategoryHelp |
获取类别的帮助文字。 |
CategoryName |
获取或设置定义此类别的性能对象的名称。 |
CategoryType |
获取性能计数器类别类型。 |
MachineName |
获取或设置此类别所在的计算机的名称。 |