How to Use Performance Counters with .NET Core
How to Use Performance Counters with .NET Core
Performance counters are handy for monitoring and troubleshooting applications. In this post, we take a look at how to use them in .NET Core, along with taking a look at an alternative to them, and where Microsoft is taking them in the future.
Join the DZone community and get the full member experience.Join For Free
Sensu is an open source monitoring event pipeline. Try it today.
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 use of them. The new Core CLR does not support them since they are not cross-platform.
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 netcoreapp.
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 ContentDashboard.exe to run kestrel. For my example, you can now see my test app “ContentDashboard” as having performance counters in the Windows perform below. W00t!
This also makes it impossible for you to create your own custom performance counters.
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, set up 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.
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
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..
Published at DZone with permission of Matt Watson , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.