Reputation: 20464
In C# or else VB.Net, and having only a PID of a process, I wonder if it can be possibly to check at execution time whether the associated process has performance counters enabled.
I'll mean when the performanceCounters
setting is enabled in its app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
...
<system.net>
<settings>
<performanceCounters enabled="true"/>
</settings>
</system.net>
...
</configuration>
However, I'm asking about the possible existance of a proper/built-in solution using reflection, or other .Net Framework members than doing a primitive check for an app.config file and then parsing the file to find the setting, I'm aware of that, its what I'm trying to avoid.
As a secondary question I will ask:
How I could check for the same thing in the current process?, I ask this because maybe the methodology to determine whether performance counters are enabled in the current process could be easier than determining it in an external process (but again I'm asking this for a solution to avoid parsing the app.config file).
Upvotes: 1
Views: 289
Reputation: 20464
I did this Generic usage function for future needs with App config file, maybe it will not be able to parse the tree level architecture in all scenarios, but hey, its just a start.
Usage:
GetAppConfigSetting(Of Boolean)("system.net", "settings", "performanceCounters", "enabled"))
Source:
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
Upvotes: 0
Reputation: 49270
You specifically want to avoid parsing the app.config file, but frankly I would. Your question suggest you don't want to "manually" parse the app.config, which you don't have to (So I'll be stubborn on suggest the following ;-))
Check for the current process:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var group = (NetSectionGroup)config.GetSectionGroup("system.net");
if (group.Settings.PerformanceCounters.Enabled)
{
Console.WriteLine("ENABLED");
}
Check for other processes, well executables really.
var config = ConfigurationManager.OpenExeConfiguration(@" ... path to other executable ... ");
var group = (NetSectionGroup)config.GetSectionGroup("system.net");
if (group.Settings.PerformanceCounters.Enabled)
{
Console.WriteLine("ENABLED");
}
Upvotes: 2
Reputation: 115779
Usually all per-process Performance Counters have PID (or process name or some other identifying information) embedded in Performance Counters Instance Names:
(the part highlighted in yellow is the PID).
So if Process ID is what you have, you can search around instance names for this substring.
Upvotes: 1