通过Web应用创建自定义计数器

时间:2008-09-11 14:21:34

标签: .net

我有一个.NET 3.5 Web应用程序,我已经实现了一个名为CalculationsCounterManager的类(下面的代码)。此类具有一些共享/静态成员,用于管理两个自定义性能计数器的创建和增量,这两个自定义性能计数器监视对SQL Server数据库的数据调用。如果不存在,执行这些数据调用会驱动计数器的创建。当然,通过这个类的nUnit GUI执行的单元测试,一切正常。计数器已创建并递增。

但是,当通过ASPNET工作进程执行相同的代码时,会出现以下错误消息:“不允许请求的注册表访问。”。当读取完成以检查计数器类别是否已存在时,会在CalculationsCounterManager类的第44行发生此错误。

有没有人知道如何为工作进程帐户提供足够的priveledges,以便允许它在生产环境中创建计数器而无需打开服务器以解决安全问题?

Namespace eA.Analytics.DataLayer.PerformanceMetrics
    ''' <summary>
    ''' Manages performance counters for the calculatioins data layer assembly
    ''' </summary>
    ''' <remarks>GAJ 09/10/08 - Initial coding and testing</remarks>
    Public Class CalculationCounterManager

        Private Shared _AvgRetrieval As PerformanceCounter
        Private Shared _TotalRequests As PerformanceCounter
        Private Shared _ManagerInitialized As Boolean
        Private Shared _SW As Stopwatch

        ''' <summary>
        ''' Creates/recreates the perf. counters if they don't exist
        ''' </summary>
        ''' <param name="recreate"></param>
        ''' <remarks></remarks>
        Public Shared Sub SetupCalculationsCounters(ByVal recreate As Boolean)

            If PerformanceCounterCategory.Exists(CollectionSettings.CalculationMetricsCollectionName) = False Or recreate = True Then

                Dim AvgCalcsProductRetrieval As New CounterCreationData(CounterSettings.AvgProductRetrievalTimeCounterName, _
                                                                        CounterSettings.AvgProductRetrievalTimeCounterHelp, _
                                                                        CounterSettings.AvgProductRetrievalTimeCounterType)

                Dim TotalCalcsProductRetrievalRequests As New CounterCreationData(CounterSettings.TotalRequestsCounterName, _
                                                                                  CounterSettings.AvgProductRetrievalTimeCounterHelp, _
                                                                                  CounterSettings.TotalRequestsCounterType)

                Dim CounterData As New CounterCreationDataCollection()

                ' Add counters to the collection.
                CounterData.Add(AvgCalcsProductRetrieval)
                CounterData.Add(TotalCalcsProductRetrievalRequests)

                If recreate = True Then
                    If PerformanceCounterCategory.Exists(CollectionSettings.CalculationMetricsCollectionName) = True Then
                        PerformanceCounterCategory.Delete(CollectionSettings.CalculationMetricsCollectionName)
                    End If
                End If

                If PerformanceCounterCategory.Exists(CollectionSettings.CalculationMetricsCollectionName) = False Then
                    PerformanceCounterCategory.Create(CollectionSettings.CalculationMetricsCollectionName, CollectionSettings.CalculationMetricsDescription, _
                                                  PerformanceCounterCategoryType.SingleInstance, CounterData)
                End If

            End If

            _AvgRetrieval = New PerformanceCounter(CollectionSettings.CalculationMetricsCollectionName, CounterSettings.AvgProductRetrievalTimeCounterName, False)
            _TotalRequests = New PerformanceCounter(CollectionSettings.CalculationMetricsCollectionName, CounterSettings.TotalRequestsCounterName, False)
            _ManagerInitialized = True

        End Sub

        Public Shared ReadOnly Property CategoryName() As String
            Get
                Return CollectionSettings.CalculationMetricsCollectionName
            End Get
        End Property

        ''' <summary>
        ''' Determines if the performance counters have been initialized
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared ReadOnly Property ManagerInitializaed() As Boolean
            Get
                Return _ManagerInitialized
            End Get
        End Property

        Public Shared ReadOnly Property AvgRetrieval() As PerformanceCounter
            Get
                Return _AvgRetrieval
            End Get
        End Property

        Public Shared ReadOnly Property TotalRequests() As PerformanceCounter
            Get
                Return _TotalRequests
            End Get
        End Property

        ''' <summary>
        ''' Initializes the Average Retrieval Time counter by starting a stopwatch
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared Sub BeginIncrementAvgRetrieval()

            If _SW Is Nothing Then
                _SW = New Stopwatch
            End If

            _SW.Start()

        End Sub

        ''' <summary>
        ''' Increments the Average Retrieval Time counter by stopping the stopwatch and changing the
        ''' raw value of the perf counter.
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared Sub EndIncrementAvgRetrieval(ByVal resetStopwatch As Boolean, ByVal outputToTrace As Boolean)
            _SW.Stop()
            _AvgRetrieval.RawValue = CLng(_SW.ElapsedMilliseconds)
            If outPutToTrace = True Then
                Trace.WriteLine(_AvgRetrieval.NextValue.ToString)
            End If
            If resetStopwatch = True Then
                _SW.Reset()
            End If
        End Sub

        ''' <summary>
        ''' Increments the total requests counter
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared Sub IncrementTotalRequests()
            _TotalRequests.IncrementBy(1)
        End Sub

        Public Shared Sub DeleteAll()
            If PerformanceCounterCategory.Exists(CollectionSettings.CalculationMetricsCollectionName) = True Then
                PerformanceCounterCategory.Delete(CollectionSettings.CalculationMetricsCollectionName)
            End If
        End Sub

    End Class
End Namespace

1 个答案:

答案 0 :(得分:3)

是的,不可能。如果不打开服务器直到生产环境中的潜在安全/ DOS问题,则无法向工作进程添加权限。安装程序(如MSI)通常以提升的权限运行,并安装/卸载性能计数器类别和计数器以及其他锁定对象。

例如,Windows Installer XML (WiX) has support for Performance Counters ...