2011-10-03 9 views
7

में कस्टम प्रदर्शन काउंटर हाय मैं पेर्फॉन में उपयोग के लिए कस्टम प्रदर्शन काउंटर बनाने की कोशिश कर रहा हूं। निम्नलिखित कोड बहुत अच्छी तरह से काम करता है, हालांकि मुझे एक समस्या है ..सी #/परफॉर्म

इस समाधान के साथ मेरे पास प्रदर्शन काउंटर के मूल्य को अपडेट करने वाला एक टाइमर है, हालांकि मुझे आवश्यक डेटा प्राप्त करने के लिए यह निष्पादन योग्य चलाने की आवश्यकता नहीं है । यही है कि मैं सिर्फ एक बार चीज के रूप में काउंटर स्थापित करने में सक्षम होना चाहता हूं और उसके बाद डेटा के लिए परफॉर्म क्वेरी है (जैसा कि यह सभी पूर्व-स्थापित काउंटरों के साथ होता है)।

मैं इसे कैसे प्राप्त कर सकता हूं?

using System; 
using System.Diagnostics; 
using System.Net.NetworkInformation; 

namespace PerfCounter 
{ 
class PerfCounter 
{ 
    private const String categoryName = "Custom category"; 
    private const String counterName = "Total bytes received"; 
    private const String categoryHelp = "A category for custom performance counters"; 
    private const String counterHelp = "Total bytes received on network interface"; 
    private const String lanName = "Local Area Connection"; // change this to match your network connection 
    private const int sampleRateInMillis = 1000; 
    private const int numberofSamples = 100; 

    private static NetworkInterface lan = null; 
    private static PerformanceCounter perfCounter; 

    static void Main(string[] args) 
    { 
     setupLAN(); 
     setupCategory(); 
     createCounters(); 
     updatePerfCounters(); 
    } 

    private static void setupCategory() 
    { 
     if (!PerformanceCounterCategory.Exists(categoryName)) 
     { 
      CounterCreationDataCollection counterCreationDataCollection = new CounterCreationDataCollection(); 
      CounterCreationData totalBytesReceived = new CounterCreationData(); 
      totalBytesReceived.CounterType = PerformanceCounterType.NumberOfItems64; 
      totalBytesReceived.CounterName = counterName; 
      counterCreationDataCollection.Add(totalBytesReceived); 
      PerformanceCounterCategory.Create(categoryName, categoryHelp, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection); 
     } 
     else 
      Console.WriteLine("Category {0} exists", categoryName); 
    } 

    private static void createCounters() { 
     perfCounter = new PerformanceCounter(categoryName, counterName, false); 
     perfCounter.RawValue = getTotalBytesReceived(); 
    } 

    private static long getTotalBytesReceived() 
    { 
     return lan.GetIPv4Statistics().BytesReceived; 
    } 

    private static void setupLAN() 
    { 
     NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); 
     foreach (NetworkInterface networkInterface in interfaces) 
     { 
      if (networkInterface.Name.Equals(lanName)) 
       lan = networkInterface; 
     } 
    } 
    private static void updatePerfCounters() 
    { 
     for (int i = 0; i < numberofSamples; i++) 
     { 
      perfCounter.RawValue = getTotalBytesReceived(); 
      Console.WriteLine("perfCounter.RawValue = {0}", perfCounter.RawValue); 
      System.Threading.Thread.Sleep(sampleRateInMillis); 
     } 
    } 
} 

}

उत्तर

6

Win32 में, प्रदर्शन काउंटर परफ़ॉर्मेंस एक DLL जो काउंटर मूल्यों प्रदान करता है लोड होने से काम करते हैं।

.NET में, यह डीएलएल एक स्टब है जो चल रही .NET प्रक्रिया के साथ संवाद करने के लिए साझा स्मृति का उपयोग करता है। प्रक्रिया समय-समय पर साझा मानकों को नए मानों को धक्का देती है, और डीएलएल उन्हें प्रदर्शन काउंटर के रूप में उपलब्ध कराता है।

तो, मूल रूप से, आपको शायद मूल कोड में अपना प्रदर्शन काउंटर डीएलएल लागू करना होगा, क्योंकि .NET प्रदर्शन काउंटर मानते हैं कि एक प्रक्रिया चल रही है।