Att skriva en Performance Counter med EntLib

Genom att ta hälp av Enterprise Library för att skriva Performance Conters så blir det väldigt enkelt. Koden nedan skapar en Performance Counter för antal sökningar per sekund.
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation
Imports System.Diagnostics

Namespace Instrumentation

    ''' <summary>
    ''' Performance counters för resesök anrop
    ''' </summary>
    ''' <remarks>
    ''' <para>Anger antal anrop per sekund.</para>
    '''</remarks>
    <HasInstallableResources()> _
    <PerformanceCountersDefinition(ConnectionSearchCounters.PerformanceCategory, "ConnectionSearchCountersHelpResource")> _
    Public Class ConnectionSearchCounters

        Public Const PerformanceCategory As String = "ConnectionSearch Counters"
        Private performanceCountersEnabled As Boolean = My.Settings.PerformanceCountersEnabled

        <PerformanceCounter("ConnectionSearches/sec", "ConnectionSeachesPerSecHelpResource", PerformanceCounterType.RateOfCountsPerSecond32)> _
        Private connectionSearchesPerSec As EnterpriseLibraryPerformanceCounter

        ''' <summary>
        ''' Default konstruktor
        ''' </summary>
        Public Sub New()
            If performanceCountersEnabled Then
                connectionSearchesPerSec = New EnterpriseLibraryPerformanceCounter(PerformanceCategory, "ConnectionSearches/sec")
            End If
        End Sub

        ''' <summary>
        ''' Adderar performance countern för ConnectionSearchPerformed
        ''' <summary>
        Public Sub ConnectionSearchPerformed()
            If performanceCountersEnabled Then
                connectionSearchesPerSec.Increment()
            End If
        End Sub
    End Class
End Namespace
 
För att använda Performance Counters så måste dom först installeras. Använd InstallUtil.exe.
 
För att sedan använda den så instanciera den och anropa sedan ConnectionSearchPerformed()
 
Nu skall det finnas ett värde i Perfmon
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s