如何从 MS Test 中的单元测试中读取“.NET Data Provider for SqlServer”类别的性能计数器“NumberOfActiveConnections”?
我正在尝试以下操作,但似乎我弄错了实例名称。MSDN 文档声称这是获取 WinForms 应用程序实例名称的正确方法,但这不适用于 MS Test:
string instanceName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
从 MS Test 运行上面的代码时,我null
从调用中返回GetEntryAssembly()
我也尝试过使用 MS 测试过程的名称和其他变体,但没有任何运气。
这是当我使用上面的任何实例名称时将引发异常的示例代码:
PerformanceCounter counter = new PerformanceCounter(
".NET Data Provider for SqlServer",
"NumberOfActiveConnections",
instanceName,
true);
Assert.AreEqual<long>(0, counter.RawValue);
根据MSDN 文档,我通过将其添加到 app.config 来启用“NumberOfActiveConnections”计数器:
<system.diagnostics>
<switches>
<add name="ConnectionPoolPerformanceCounterDetail" value="4"/>
</switches>
</system.diagnostics>
也许问题是性能计数器是为 MS 测试主机域启用的,但不是为实际运行测试的域启用的?