gpt4 book ai didi

.net - 通过 Web 应用程序创建自定义计数器

转载 作者:行者123 更新时间:2023-12-04 07:12:35 25 4
gpt4 key购买 nike

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

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

有没有人知道一种方法可以为工作进程帐户提供足够的特权,以允许它在生产环境中创建计数器而不会打开服务器以解决安全问题?

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

最佳答案

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

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

关于.net - 通过 Web 应用程序创建自定义计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com