How do I read from the performance counter "NumberOfActiveConnections" of the category ".NET Data Provider for SqlServer" from a unit test in MS Test?
I'm trying the following but it seems like I get the instance name wrong. The MSDN documentation claims this is a correct way of getting the instance name of a WinForms app but this won't work with MS Test:
string instanceName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
When running the code above from MS Test I get null
back from the call to GetEntryAssembly()
I've also tried using the name of the MS Test process and other variations but without any luck.
This is the sample code that will throw exception when i'm using any of the instance names from above:
PerformanceCounter counter = new PerformanceCounter(
".NET Data Provider for SqlServer",
"NumberOfActiveConnections",
instanceName,
true);
Assert.AreEqual<long>(0, counter.RawValue);
I'm enabling the "NumberOfActiveConnections" counter by adding this to the app.config as per MSDN documentation:
<system.diagnostics>
<switches>
<add name="ConnectionPoolPerformanceCounterDetail" value="4"/>
</switches>
</system.diagnostics>
Perhaps the problem is that the performance counters are enabled for the MS Test host domain but not for the domain in which the tests actually run?