[英]Check if current and external .Net process has performance counters enabled?
在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文件)。
你特别希望避免解析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");
}
通常,所有每个进程的性能计数器都具有嵌入在性能计数器实例名称中的PID(或进程名称或一些其他标识信息):
(以黄色突出显示的部分是PID)。
因此,如果您拥有Process ID,则可以搜索此子字符串的实例名称。
我使用App配置文件为未来需求做了这个通用使用功能,也许它无法在所有场景中解析树级架构,但是,嘿,它只是一个开始。
用法:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.