Retrace will reach End of Life on March 31, 2027. Click here to learn more.

How to Use Performance Counters with .NET Core: Current Solution, Alternatives, and the Future

By: mwatson
  |  November 7, 2017
How to Use Performance Counters with .NET Core: Current Solution, Alternatives, and the Future

Performance counters are really important for monitoring and troubleshooting problems with your .NET applications. The full .NET Framework provides a wide array of performance counters that are very useful for troubleshooting application problems.

Some examples of important performance counters are garbage collection and exception rates. Without these, you will be flying blind.

In this article we will discuss how to use performance counters with .NET Core, an alternative to these performance counters, and Microsoft’s future replacement.

How to use performance counters with .NET Core

Using performance counters with .NET Core is actually quite easy, if you plan on deploying to Windows. All you need to do is change your application to target the full .NET Framework. The original .NET CLR supports them and makes extensive usage of them. The new Core CLR does not support them since they are not cross-platform. You can use .NET Core and ASP.NET Core with the full .NET Framework on Windows!

If you need support for performance counters, deploying your application to Linux or macOS is not an option. Since Linux or macOS do not have performance counters because they are very much a Windows thing, there is no support for them in apps that target netcoreapp, which is the Core CLR.

Switch your ASP.NET Core project from Core CLR to the full framework .NET CLR

If you want to use .NET Core on Windows and have access to performance counters, you can do so by switching your ASP.NET Core web application from targeting netcoreapp20 to net471.

Find your csproj file in Windows Explorer and edit it with a text editor. Change the “TargetFramework” to net471 so it uses the full .NET Framework.

<Project Sdk="Microsoft.NET.Sdk.Web">
   <PropertyGroup>
      <TargetFramework>net471</TargetFramework>
   </PropertyGroup>
   …
</Project>

I made the change, published it, and ran my test application, ContentDashboard.exe from Visual Studio. This started the app with the full .NET Framework and the kestrel web server. For my test, you can now see my test app “ContentDashboard” as having performance counters in Windows perfmon below. w00t!

 see my test app “ContentDashboard” as having performance counters in Windows perform below. w00t!

Beyond the built-in metrics that the .NET framework provides, you can also make your own custom performance counters. Unfortunately, if you can’t use performance counters with .NET Core, you also can’t use them for your own custom metrics. Luckily, there are alternative solutions for your own custom metrics!

[adinserter block=”33″]

Alternative to performance counters for custom metrics within your application

If you want to use performance counters for your own custom application metrics, you can instead use a 3rd-party library for recording your metrics.

Our product Retrace has excellent support for custom metrics. With just a couple lines of code, you can implement several different types of custom application metrics for your application.

Here is an example of how to report a custom metric to Retrace:

StackifyLib.Metrics.Count(“Logs Processor”, “Incoming App Log Count”, 1);

Your metrics will now show up within Retrace and you can compare the metric across servers, setup alerts, etc.

Your metrics will now show up within Retrace and you can compare the metric across servers, setup alerts, etc.

This is a much simpler solution than using Windows performance counters. They require creating them, reporting data to them, and then configuring your application monitoring system to monitor them. Retrace just does all of that with the one line of code.

The future solution from Microsoft is EventCounters

Microsoft has so far said that performance counters are not coming to the Core CLR. Instead, a new type of counters called EventCounters are the future.

EventCounters are cross-platform and work on top of EventSource and Event tracing for Windows (ETW). The problem is that they aren’t really in use anywhere and there doesn’t seem to be much tooling in place to use them.

A simple Google search for EventCounters doesn’t even bring up much at all. I’m not sure when they will be available. Perhaps in .NET Core 2.1?

This particular comment on GitHub does a good job of describing them and why Microsoft is going this direction.

Here is one page that shows an example: Introduction Tutorial: How to measure performance for very frequent events using EventCounters

Summary

If you want to monitor the performance of your application and troubleshoot things like garbage collection and exception rates, performance counters are critical. For now, the simplest solution is targeting the full .NET Framework and running your application on Windows.

In the future, EventCounters will be able to provide similar types of metrics about the performance of your .NET application from the .NET internals themselves.

In a future article I will try and use EventCounters myself and see if I can get them working! I’d love to see if they show statistics for things like garbage collection, exception rates, etc.

If you are wanting to create your own custom application metrics, a good alternative is using a 3rd-party solution like Retrace.

Also Read-https://stackify.com/dependency-injection-c-sharp/

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More
togel slot situs toto pucuk4d agb99 toto slot toto togel indobet situs toto toto slot toto slot toto slot toto toto slot slot depo 10k toto togel 8kuda4d toto judi bola kenangan4d toto situs toto toto toto toto situs toto toto toto slot bobatoto daftar situs toto logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto slot gacor terbaru toto toto situs toto situs toto MORFINTOTO ilmutoto toto mix parlay bayitoto toto toto toto toto ilmutoto toto toto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot pucuk4d lingkartoto Pucuk4D Login UGSLOT slot gacor ilmutoto https://foundvinylrecords.com/ depo 5k toto slot gacor panen100 semibola Pakde4D situs gacor MySlot188 toto togel 77ag monk4d popo togel tikus4d https://asupantoto.co/ Login Batak5D pompa4d lingkartoto Pakde4D lingkartoto emas 55 AMANAHTOTO efekjitu slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 benteng786 dinasti33 slot slot gacor kaskustoto situs toto slot88 slot gacor slot jepang slot depo 5k pucuk4d patentoto https://www.juara288.asia/ flokitoto https://www.aishe-j.org/ barges88 mataramtoto https://shopjoliboutique.com/ apo388 slot 4d RP888 toto toto toto toto toto slot gacor toto mataramtoto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto slot gacor sontogel PASCOL4D LOGIN mpo slot sumbartoto mataramtoto slot gacor situs toto slot situs slot situs toto pakde4d slot toto link slot toto sumbartoto Login bandar toto
togel slot situs toto pucuk4d agb99 toto slot toto togel indobet situs toto toto slot toto slot toto slot toto toto slot slot depo 10k toto togel 8kuda4d toto judi bola kenangan4d toto situs toto toto toto toto situs toto toto toto slot bobatoto daftar situs toto logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto slot gacor terbaru toto toto situs toto situs toto MORFINTOTO ilmutoto toto mix parlay bayitoto toto toto toto toto ilmutoto toto toto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot pucuk4d lingkartoto Pucuk4D Login UGSLOT slot gacor ilmutoto https://foundvinylrecords.com/ depo 5k toto slot gacor panen100 semibola Pakde4D situs gacor MySlot188 toto togel 77ag monk4d popo togel tikus4d https://asupantoto.co/ Login Batak5D pompa4d lingkartoto Pakde4D lingkartoto emas 55 AMANAHTOTO efekjitu slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 benteng786 dinasti33 slot slot gacor kaskustoto situs toto slot88 slot gacor slot jepang slot depo 5k pucuk4d patentoto https://www.juara288.asia/ flokitoto https://www.aishe-j.org/ barges88 mataramtoto https://shopjoliboutique.com/ apo388 slot 4d RP888 toto toto toto toto toto slot gacor toto mataramtoto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto slot gacor sontogel PASCOL4D LOGIN mpo slot sumbartoto mataramtoto slot gacor situs toto slot situs slot situs toto pakde4d slot toto link slot toto sumbartoto Login bandar toto