System.Diagnostics.PerformanceData 命名空間

使用此命名空間中的類別以提供計數器資料。 計數器是用來向取用者公開效能指標,例如效能監視器。 命名空間未包含取用計數器資料的類別。 如需效能計數器架構的完整說明,請參閱效能計數器

類別

CounterData

包含計數器的未經處理資料 (Raw Data)。

CounterSet

定義一組邏輯計數器。

CounterSetInstance

建立 CounterSet 類別中定義之邏輯計數器的執行個體。

CounterSetInstanceCounterDataSet

包含計數器值的集合。

列舉

CounterSetInstanceType

指定計數器集合可允許多重執行個體 (Instance),例如處理序 (Process) 和實體磁碟,還是單一執行個體 (例如記憶體)。

CounterType

定義計數器的可能型別。 每個計數器都會指派一種計數器型別。 此計數器型別決定計數器資料的計算、求平均值及顯示等方式。

範例

以下顯示簡單的資訊清單:

<!-- <?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>  

以下顯示資訊清單的簡單提供者實作:

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 版) 。 在新架構中,提供者不再直接回應取用者要求,而是只會維護計數器資料。 當提供者建立計數器集的實例時,系統會將執行緒插入提供者的進程;執行緒負責處理取用者要求。

下列步驟顯示撰寫計數器提供者的程式。

  1. 提供者提供的計數器定義于 XML 型資訊清單中。 計數器會以邏輯方式分組為計數器集合。 計數器集合內的計數器是由計數器集合內唯一的數值識別碼來識別。 提供者可以定義一或多個計數器集合。 計數器集合是由提供者唯一的 Guid 所識別。 請注意,如果您使用這些類別來撰寫提供者:

    • 忽略提供者專案的回呼屬性。

    • 會忽略 counterAttribute 元素之 name 屬性的參考值。

    如需撰寫資訊清單的詳細資訊,請參閱 效能計數器架構

  2. 撰寫資訊清單之後,請使用 CTRPP 工具來編譯資訊清單, (ctrpp provider.man) 。 此工具會產生四個檔案:.h、.c、.rc 和 *_r.h。 您可以忽略 .h 和 .c 檔案。 .rc 檔案包含資訊清單中定義的當地語系化字串。 您可以使用 .rc 和 *_r.h 檔案來建立編譯的資源檔, (.res) 包含在專案中。 下列呼叫示範如何編譯資源檔:

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

    如果您收到參考 sal.h 的錯誤,請從 Microsoft Visual Studio 複製 sal.h 檔案,Visual C include 目錄到您為 /i 參數指定的目錄。

    將已編譯資源檔的路徑新增 (.res) 至專案的 [應用程式] 屬性頁。

  3. 撰寫您的提供者。 下列步驟顯示提供者所進行的呼叫:

    1. 呼叫建 CounterSet.CounterSet 構函式以定義計數器集合。 針對資訊清單中定義的每個計數器集呼叫這個方法。

    2. 針對每個計數器集合,呼叫其中 CounterSet.AddCounter 一個方法,將計數器新增至集合。 針對計數器集合中定義的每個計數器呼叫這個方法。

    3. CounterSet.CreateCounterSetInstance呼叫 方法來建立計數器集的實例, (實例包含計數器資料) 。 針對單一實例計數器集合,請呼叫此方法一次。 對於多個實例計數器集合,請針對您需要提供計數器資料的每個實例呼叫這個方法, (針對每個實例使用唯一的名稱) 。

    4. CounterSetInstance.Counters使用 屬性來存取和設定計數器的計數器資料。

  4. 完成提供者之後,請使用 LodCtr 工具在電腦上註冊計數器。 例如,

    lodctr /m:provider.man  
    

    此範例假設資訊清單和可執行檔位於目前目錄中。

您可以在執行 Windows Vista 和更新版本的作業系統的電腦上,使用此命名空間中的類別。