April 18, 2016

665 Views

Using C# to Monitor Microsoft Server using Performance Counters

In today’s world, most of the businesses run on IT infrastructure and network. And stability is a key factor to run the businesses. Because the cost of downtime not only impacts the business’ productivity but also its service and reputation.

So, it is necessary to continuously monitor the server and detect the issue and possible causes to prevent any major issues before occurring.

To monitor Microsoft server performance, performance counters can be used. Basically, there are several category of performance counters which are used for Microsoft server monitoring. Each application which runs on the server has a type and is treated as a separate category. For a single performance category, there are several performance counters available. To monitor any category/application, we use these counters. These counters have a value based on the type, we can check the value to monitor the particular behavior of that category.

In C# server monitoring, we use a namespace called “PerformanceCounterCategory”System.Diagnostics” for this purpose. In this namespace, there is one class called “PerformanceCounterCategory” class through which we can monitor the performance through performance counters.

Following are the steps through which we can collect the actual run time data for any counter which is used for Microsoft server monitoring:

  1. Get all the performance categories using “PerformanceCounterCategory” class and GetCategories() method.
  2. Select a particular performance category to monitor.
  3. Get the instances of selected performance category, if any using “PerformanceCounterCategory” and GetInstanceNames() method.
  4. Based on the selected category and instance, get all performance counters using “PerformanceCounter” class.
  5. Select a particular counter, and get the actual run time value of that counter, using the NextValue() method.
  6. Compare the value of the counter with your desired value and raise an alert, if it mismatches.
    Now based on the selection, we can monitor any counter and check the value. We have to write our own logic in C# to compare the value of counter with the desired value. Based on the result, we can further save it or send any alert.

References