System.Diagnostics.PerformanceData 命名空间

使用此命名空间中的类来提供计数器数据。 Use the classes in this namespace to provide counter data. 计数器用于向用户公开性能指标,如性能监视器。 The counters are used to expose performance metrics to consumers such as the Performance Monitor. 命名空间不包含用于使用计数器数据的类。 The namespace does not contain classes for consuming the counter data. 有关性能计数器体系结构的完整说明,请参阅 Performance Counters.aspx)(性能计数器)。 For a complete description of the performance counters architecture, see Performance Counters.aspx).

CounterData

包含计数器的原始数据。Contains the raw data for a counter.

CounterSet

定义一组逻辑计数器。Defines a set of logical counters.

CounterSetInstance

创建在 CounterSet 类中定义的逻辑计数器的实例。Creates an instance of the logical counters defined in the CounterSet class.

CounterSetInstanceCounterDataSet

包含计数器值的集合。Contains the collection of counter values.

枚举

CounterSetInstanceType

指定计数器集是允许多实例(如进程和物理磁盘),还是允许单实例(如内存)。Specifies whether the counter set allows multiple instances such as processes and physical disks, or a single instance such as memory.

CounterType

定义可能的计数器类型。 每个计数器都被分配一个计数器类型。 计数器类型用来确定如何计算和显示计数器数据以及如何对其求平均值。The counter type determines how the counter data is calculated, averaged, and displayed.

示例

下面显示了一个简单的清单:The following shows a simple manifest:

<!-- <?xml version="1.0" encoding="UTF-16"?> -->  
<instrumentationManifest xsi:schemaLocation="http://schemas.microsoft.com/win/2004/08/events eventman.xsd"   
     xmlns:win="http://manifests.microsoft.com/win/2004/08/windows/events"   
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
     xmlns:xs="http://www.w3.org/2001/XMLSchema"   
     xmlns:trace=http://schemas.microsoft.com/win/2004/08/events/trace>  
  
    <instrumentation>  
  
        <counters xmlns=http://schemas.microsoft.com/win/2005/12/counters>  
  
            <provider  
              applicationIdentity = "provider1.exe"  
              providerType = "userMode"  
              providerGuid = "{51D1685C-35ED-45be-99FE-17261A4F27F3}">  
  
               <counterSet guid = "{582803C9-AACD-45e5-8C30-571141A22092}"  
                  uri = "Microsoft.Windows.System.PerfCounters.Typing"  
                  name = "$(string.CounterSet1.Name)"   
                  description = "$(string.CounterSet1.Description)"   
                  instances = "single">  
  
                    <counter id = "1"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.TotalWords"  
                      name = "$(string.CS1.Counter1.Name)"  
                      description = "$(string.CS1.Counter1.Description)"  
                      type = "perf_counter_rawcount"  
                      detailLevel = "standard"/>  
  
                    <counter id = "2"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.WordsInInterval"  
                      name = "$(string.CS1.Counter2.Name)"  
                      description = "$(string.CS1.Counter2.Description)"  
                      type = "perf_counter_delta"  
                      detailLevel = "standard"/>  
  
                    <counter id = "3"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.LetterAPressed"  
                      name = "$(string.CS1.Counter3.Name)"  
                      description = "$(string.CS1.Counter3.Description)"  
                      type = "perf_counter_rawcount"  
                      detailLevel = "standard"/>  
  
                    <counter id = "4"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.WordsContainingLetterA"  
                      name = "$(string.CS1.Counter4.Name)"   
                      description = "$(string.CS1.Counter4.Description)"   
                      type = "perf_counter_rawcount"  
                      detailLevel = "standard"/>  
  
                    <counter id = "5"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.PercentOfWordsContainingLetterA"  
                      name = "$(string.CS1.Counter5.Name)"   
                      description = "$(string.CS1.Counter5.Description)"   
                      type = "perf_sample_fraction"  
                      baseID = "6"  
                      detailLevel = "standard">  
                      <counterAttributes>  
                          <counterAttribute name = "displayAsReal" />  
                      </counterAttributes>  
                    </counter>  
  
                    <counter id = "6"  
                      uri = "Microsoft.Windows.System.PerfCounters.Typing.PercentBase"  
                      type = "perf_sample_base"  
                      detailLevel = "standard">  
                      <counterAttributes>  
                          <counterAttribute name = "noDisplay" />  
                      </counterAttributes>  
                    </counter>  
  
                </counterSet>  
            </provider>  
        </counters>  
    </instrumentation>  
  
    <localization>  
        <resources culture="en-US">  
            <stringTable>  
  
                <string id="CounterSet1.Name" value="Typing"/>  
                <string id="CounterSet1.Description" value="Captures simple typing metrics."/>  
                <string id="CS1.Counter1.Name" value="Total Words Typed"/>   
                <string id="CS1.Counter1.Description" value="The total number of words typed."/>  
                <string id="CS1.Counter2.Name" value="Words Typed In Interval"/>   
                <string id="CS1.Counter2.Description" value="The total number of words typed in the interval."/>  
                <string id="CS1.Counter3.Name" value="Letter A Pressed"/>   
                <string id="CS1.Counter3.Description" value="The number of times that the letter A is pressed."/>  
                <string id="CS1.Counter4.Name" value="Words Containing A"/>   
                <string id="CS1.Counter4.Description" value="The number of words that contain the letter A."/>  
                <string id="CS1.Counter5.Name" value="Percent of Words Containing A"/>   
                <string id="CS1.Counter5.Description" value="The percent of words that contain the letter A in the last interval."/>  
  
            </stringTable>  
        </resources>  
    </localization>  
</instrumentationManifest>  

下面显示了清单的简单提供程序实现:The following shows a simple provider implementation for the manifest:

using System.Diagnostics.PerformanceData;  
  
        private static Guid providerId = new Guid("{51D1685C-35ED-45be-99FE-17261A4F27F3}");  
        private static Guid typingCounterSetId = new Guid("{582803C9-AACD-45e5-8C30-571141A22092}");  
  
        private static CounterSet typingCounterSet;         // Defines the counter set  
        private static CounterSetInstance typingCsInstance; // Instance of the counter set  
  
        private static int numberOfLetterAInWord = 0;  
  
        . . .  
  
            // Create the 'Typing' counter set.  
            typingCounterSet = new CounterSet(providerId, typingCounterSetId, CounterSetInstanceType.Single);  
  
            // Add the counters to the counter set definition.  
            typingCounterSet.AddCounter(1, CounterType.RawData32, "Total Word Count");  
            typingCounterSet.AddCounter(2, CounterType.Delta32, "Words Typed In Interval");  
            typingCounterSet.AddCounter(3, CounterType.RawData32, "A Key Pressed");  
            typingCounterSet.AddCounter(4, CounterType.RawData32, "Words Containing A");  
            typingCounterSet.AddCounter(5, CounterType.SampleFraction, "Percent of Words Containing A");  
            typingCounterSet.AddCounter(6, CounterType.SampleBase, "Percent Base");  
  
            // Create an instance of the counter set (contains the counter data).  
            typingCsInstance = typingCounterSet.CreateCounterSetInstance("Typing Instance");  
            typingCsInstance.Counters[1].Value = 0;  
            typingCsInstance.Counters[2].Value = 0;  
            typingCsInstance.Counters[3].Value = 0;  
            typingCsInstance.Counters[4].Value = 0;  
            typingCsInstance.Counters[5].Value = 0;  
            typingCsInstance.Counters[6].Value = 0;  
  
        . . .  
  
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
        {  
            typingCounterSet.Dispose();  
        }  
  
        // Simple effort to capture letter A key press and words typed.  
        private void textInput_KeyDown(object sender, KeyEventArgs e)  
        {  
            Keys keyData = e.KeyData;  
  
            switch (e.KeyData)  
            {  
                case Keys.A :  
                    // In the .NET 3.5 Framework, you had to use the  
                    // Value property to set and increment the counter   
                    // value. Beginning with the .NET 4.0 Framework,   
                    // the Value property is safe to use in a multi-  
                    // threaded application.  
                    typingCsInstance.Counters["A Key Pressed"].Value++;  
                    numberOfLetterAInWord++;  
  
                    break;  
  
                case Keys.Enter:  
                case Keys.Space:  
                case Keys.Tab:  
  
                    if (numberOfLetterAInWord > 0)  
                    {  
                        // Beginning with the .NET 4.0 Framework, you   
                        // can use the Increment method to increment   
                        // the counter value by 1. The Increment method   
                        // is safe to use in a multi-threaded   
                        // application.  
                        typingCsInstance.Counters["Words Containing A"].Increment();  
                        typingCsInstance.Counters["Percent of Words Containing A"].Increment();  
                        numberOfLetterAInWord = 0;  
                    }  
  
                    typingCsInstance.Counters["Percent Base"].Increment();  
                    typingCsInstance.Counters["Total Word Count"].Increment();  
                    typingCsInstance.Counters["Words Typed In Interval"].Increment();  
  
                    break;  
            }  
        }  

注解

此命名空间中的类支持 Windows Vista 中引入的性能计数器的新体系结构 (版本 2.0)。The classes in this namespace support the new architecture (version 2.0) for performance counters that was introduced in Windows Vista. 在新的体系结构中, 提供程序不再直接响应使用者请求, 而只是维护计数器数据。In the new architecture, the provider no longer responds to consumer requests directly, but instead simply maintains the counter data. 当提供程序创建计数器集的实例时, 系统会将一个线程注入到提供程序的进程中;线程负责处理使用者请求。The system injects a thread into the provider's process when the provider creates an instance of the counter set; the thread is responsible for handling consumer requests.

以下步骤显示编写计数器提供程序的过程。The following steps show the process for writing a counters provider.

  1. 提供程序提供的计数器是在基于 XML 的清单中定义的。The counters that the provider provides is defined in an XML-based manifest. 计数器按逻辑分组为计数器集。The counters are logically grouped into counter sets. 计数器集中的计数器由计数器集内唯一的数字标识符标识。The counters within a counter set are identified by a numeric identifier that is unique within the counter set. 提供程序可以定义一个或多个计数器集。A provider can define one or more counter sets. 计数器集由提供程序的唯一 Guid 标识。A counter set is identified by a Guid that is unique to a provider. 请注意, 如果使用这些类来编写提供程序:Note that if you use these classes to write your provider:

    • 提供程序元素的回调特性被忽略。The callback attribute of the provider element is ignored.

    • 忽略 counterAttribute 元素的 name 属性的引用值。The reference value for the name attribute of the counterAttribute element is ignored.

    有关编写清单的详细信息, 请参阅性能计数器架构For details on writing the manifest, see Performance Counters Schema.

  2. 编写清单后, 使用CTRPP工具编译清单 (CTRPP 提供程序)。After writing your manifest, use the CTRPP tool to compile the manifest (ctrpp provider.man). 该工具生成四个文件: .h、.c、.rc 和 * _r。The tool generates four files: .h, .c, .rc, and *_r.h. 您可以忽略 .h 和 .c 文件。You can ignore the .h and .c files. .Rc 文件包含清单中定义的本地化字符串。The .rc file contains the localized strings defined in your manifest. 使用 .rc 和 * _r 文件可创建项目中包含的已编译资源文件 (res)。You use the .rc and *_r.h files to create the compiled resource file (.res) that you include in your project. 以下调用显示了如何编译资源文件:The following call shows how to compile the resource file:

    rc /r /i "c:\Program Files\Microsoft SDKs\Windows\v6.0\Include" provider.rc  
    

    如果收到引用 sal 的错误, 请将 Microsoft Visual Studio 的 sal .h 文件 (Visual C 包含目录) 复制到为/i 开关指定的目录中。If you get an error that references sal.h, copy the sal.h file from your Microsoft Visual Studio, Visual C include directory to the directory you specify for the /i switch.

    向项目的应用程序属性页添加已编译的资源文件 (res) 的路径。Add a path to the compiled resource file (.res) to your project's Application property page.

  3. 编写提供程序。Write your provider. 以下步骤说明了提供程序所进行的调用:The following steps show the calls made by a provider:

    1. CounterSet.CounterSet调用构造函数以定义计数器集。Call the CounterSet.CounterSet constructor to define the counter set. 为清单中定义的每个计数器集调用此方法。Call this method for each counter set defined in the manifest.

    2. 对于每个计数器集, 请调用CounterSet.AddCounter方法之一, 将计数器添加到该集。For each counter set, call one of the CounterSet.AddCounter methods to add the counters to the set. 为计数器集中定义的每个计数器调用此方法。Call this method for each counter defined in the counter set.

    3. CounterSet.CreateCounterSetInstance调用方法以创建计数器集的实例 (实例包含计数器数据)。Call the CounterSet.CreateCounterSetInstance method to create an instance of the counter set (an instance contains the counter data). 对于单实例计数器集, 请调用此方法一次。For single instance counter sets, call this method one time. 对于多个实例计数器集, 请为需要为其提供计数器数据的每个实例 (使用每个实例的唯一名称) 调用此方法。For multiple instance counter sets, call this method for each instance for which you need to provide counter data (use a unique name for each instance).

    4. CounterSetInstance.Counters使用属性可以访问和设置计数器的计数器数据。Use the CounterSetInstance.Counters property to access and set the counter data for the counter.

  4. 完成提供程序后, 请使用LodCtr工具在计算机上注册计数器。After you finish your provider, use the LodCtr tool to register the counters on the computer. 例如,应用于对象的For example,

    lodctr /m:provider.man  
    

    该示例假设清单和可执行文件位于当前目录中。The example assumes the manifest and executable file are in the current directory.

你可以在运行 Windows Vista 和更高版本操作系统的计算机上使用此命名空间中的类。You can use the classes in this namespace on computers that run the Windows Vista and later operating systems.