Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 178771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:17:21+00:00 2026-05-11T14:17:21+00:00

How do I read from the performance counter NumberOfActiveConnections of the category .NET Data

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:17:22+00:00Added an answer on May 11, 2026 at 2:17 pm

    The instance name for VSTestHost for me tends to be a long string that looks vaguely similar to a GUID followed by the PID, so figuring out the instance name is a little bit of a pain, though if you know the PID (I’ll get to that in a second), then it can be done by doing something along these lines:

    PerformanceCounterCategory category = new PerformanceCounterCategory(     '.NET Data Provider for SqlServer'); string testInstanceName = null; foreach(string instanceName in category.GetInstanceNames()) {     if (instanceName.EndsWith(string.Format('[{0}]', pid)))     {         testInstanceName = instanceName;         break;     } }  if(testInstanceName != null) {     PerformanceCounter counter = new PerformanceCounter(         category.CategoryName,         'Number of Active Connections',         testInstanceName,         true); } 

    Since calling GetEntryAssembly() during a unit test returns null, it’s also kind of a pain to get ahold of the PID for VSTestHost.exe. However, you should be able to do something along these lines:

    Process[] testProcesses = Process.GetProcessesByName('VSTestHost'); if (testProcesses.Length == 1) {     pid = testProcesses[0].Id; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read a number of performance counters from a running .NET 4
(Maybe I am doing something wrong but) I am getting awful read performance from
I am trying to read from a file and encrypt the data using AES
I'm trying to create a simple Asp.Net page to read Perfmon counters from a
I read that self-signed certificates suffer from performance issues (for example, here ) but
I am performing a scattered read of 8-bit data from a file (De-Interleaving a
I'm trying to read bytes from the accessory's InputStream without blocking the thread (if
I am trying to extract the TPS number from the following strings using Java
I'm trying to read a public MSMQ from a remote (Server 2008) machine. I
I read from Outlook Contacts my Contacts and then I add the Contact to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.