【问题标题】:Check if current and external .Net process has performance counters enabled?检查当前和外部 .Net 进程是否启用了性能计数器?
【发布时间】:2015-09-16 06:10:44
【问题描述】:

在 C# 或其他 VB.Net 中,并且只有一个进程的 PID,我想知道是否可以在执行时检查相关进程是否启用了性能计数器。

我的意思是在其 app.config 中启用performanceCounters 设置时:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
...
  <system.net>
    <settings>
      <performanceCounters enabled="true"/>
    </settings>
  </system.net>
...
</configuration>

但是,我询问是否存在使用反射或其他 .Net Framework 成员的适当/内置解决方案,而不是对 app.config 文件进行原始检查,然后解析文件以查找设置,我知道,这是我试图避免的。

作为第二个问题,我会问:

如何在当前流程中检查相同的内容? 我问这个是因为确定当前进程中是否启用性能计数器的方法可能比在外部进程中确定它更容易(但我再次要求这个解决方案以避免解析 app.config 文件)。

【问题讨论】:

    标签: c# .net vb.net reflection performancecounter


    【解决方案1】:

    您特别希望避免解析 app.config 文件,但坦率地说我会的。您的问题表明您不想“手动”解析 app.config,而您不必这样做(所以我会固执地提出以下建议;-))

    检查当前进程:

            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var group = (NetSectionGroup)config.GetSectionGroup("system.net");
            if (group.Settings.PerformanceCounters.Enabled)
            {
                Console.WriteLine("ENABLED");
            }
    

    检查其他进程,确实是可执行文件。

            var config = ConfigurationManager.OpenExeConfiguration(@" ... path to other executable ... ");
            var group = (NetSectionGroup)config.GetSectionGroup("system.net");
            if (group.Settings.PerformanceCounters.Enabled)
            {
                Console.WriteLine("ENABLED");
            }
    

    【讨论】:

    • 我喜欢这个答案,我发现了 System.Configuration.ConfigurationManager 类的强大功能,非常感谢。 (等待 StackOverflow 安全期才能给予赏金)
    【解决方案2】:

    通常所有每个进程的性能计数器都在性能计数器实例名称中嵌入了 PID(或进程名称或其他一些识别信息):

    (黄色高亮部分为PID)。

    因此,如果您拥有进程 ID,则可以在实例名称中搜索此子字符串。

    【讨论】:

    • 感谢您的回答。但为了能够看到“X”PerformanceCounterCategory 的实例名称,我认为该应用程序还需要至少运行 1 个计数器(而不仅仅是启用 performancecounters 设置),这与我的问题不同。如我错了请纠正我。再次感谢!。
    • @ElektroStudios 要存在特定 PID 的性能计数器,该进程(应用程序本身)应该正在运行,是的。
    【解决方案3】:

    我用App config文件做了这个Generic使用功能,以满足未来的需求,也许它无法在所有场景下解析树级架构,但是嘿,这只是一个开始。

    用法:

     GetAppConfigSetting(Of Boolean)("system.net", "settings", "performanceCounters", "enabled"))
    

    来源:

    Public Shared Function GetAppConfigSetting(Of T)(ByVal sectionGroupName As String,
                                                     ByVal sectionName As String,
                                                     ByVal elementName As String,
                                                     ByVal propertyName As String,
                                                     Optional ByVal exePath As String = "") As T
    
        Dim appConfig As Configuration
        Dim group As ConfigurationSectionGroup
        Dim section As ConfigurationSection
        Dim sectionPropInfo As PropertyInformation
        Dim element As ConfigurationElement
        Dim elementPropInfo As PropertyInformation
    
        If Not String.IsNullOrEmpty(exePath) Then
            appConfig = ConfigurationManager.OpenExeConfiguration(exePath)
        Else
            appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        End If
    
        group = appConfig.GetSectionGroup(sectionGroupName)
        If group Is Nothing Then
            Return Nothing
        End If
    
        section = group.Sections(sectionName)
        If section Is Nothing Then
            Return Nothing
        End If
    
        sectionPropInfo = section.ElementInformation.Properties(elementName)
        If sectionPropInfo Is Nothing Then
            Return Nothing
        End If
    
        element = DirectCast(sectionPropInfo.Value, ConfigurationElement)
        If element Is Nothing Then
            Return Nothing
        End If
    
        elementPropInfo = element.ElementInformation.Properties(propertyName)
        If elementPropInfo Is Nothing Then
            Return Nothing
        End If
    
        Return DirectCast(elementPropInfo.Value, T)
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      相关资源
      最近更新 更多