Scott Hanselman

Cross-platform diagnostic tools for .NET Core

September 15, 2020 Comment on this post [8] Posted in DotNetCore
Sponsored By

.NET Core is cross-platform and open-source. Tell someone, maybe your boss.

A good reminder. It's been this way for a half decade but I'm still bumping into folks who have never heard this. Moving forward, .NET 5 will be a unification of the .NET Framework you may have heard for years, and the new .NET Core I like talking about, PLUS great goodness, tools and libraries from Mono and Xamarin. It's one cross-platform .NET with a number greater than 4. Because 5 > 4, natch.

NOTE: If you like, you can learn all about What is .NET? over on my YouTube.

Now you've made some software, maybe for Windows, maybe Mac, maybe Linux. There's a lot of ways to diagnose your apps in .NET Core, from the Docs:

  • Logging and tracing are related techniques. They refer to instrumenting code to create log files. The files record the details of what a program does. These details can be used to diagnose the most complex problems. When combined with time stamps, these techniques are also valuable in performance investigations.
  • Unit testing is a key component of continuous integration and deployment of high-quality software. Unit tests are designed to give you an early warning when you break something.
  • Debug Linux dumps explains how to collect and analyze dumps on Linux.

But I want to talk about the...

.NET Core Diagnostic Global Tools

First, let's start with...

dotnet-counters

dotnet tool install --global dotnet-counters

Now that I've installed it, I can see what .NET Core apps I'm running, like a local version of my Hanselminutes podcast site.

dotnet counters ps
18996 hanselminutes.core D:\github\hanselminutes-core\hanselminutes.core\bin\Debug\netcoreapp3.1\hanselminutes.core.exe
14376 PowerLauncher C:\Program Files\PowerToys\modules\launcher\PowerLauncher.exe
24276 pwsh C:\Program Files\PowerShell\7\pwsh.exe

I also see PowerShell 7 in there that I'm running in Windows Terminal. Pwsh is also written in cross platform .NET Core.

I'll run it again with a process id, in this case that of my podcast site:

dotnet counters monitor --process-id 18996

Here I'll get a nice constantly refreshing taskman/processmonitor of sorts in the form of dotnet-countersperformance counters:

dotnet-monitor

Again this works outside Visual Studio and it works everywhere. You can watch them and react, or collect them to a file.

dotnet-dump

The dotnet-dump tool is a way to collect and analyze Windows and Linux core dumps without a native debugger. Although it's not yet supported on macOS, it works on Windows and Linux.

With a similar syntax, I'll dump the process:

dotnet dump collect -p 18996
Writing full to D:\github\hanselminutes-core\hanselminutes.core\dump_20200918_224648.dmp
Complete

Then I'll start an interactive analysis shell session. You can run SOS (Son of Strike) commands to analyze crashes and the garbage collector (GC), but it isn't a native debugger so things like displaying native stack frames aren't supported.

dotnet dump analyze .\dump_20200918_224648.dmp
Loading core dump: .\dump_20200918_224648.dmp ...
Ready to process analysis commands. Type 'help' to list available commands or 'help [command]' to get detailed help on a command.
Type 'quit' or 'exit' to exit the session.
>

There's tons to explore. Debugging production dumps like this is a lost art.

Exploring in dotnet dump

You can also do live Garbage Collector dumps with

dotnet-gcdump

GCDump is:

"a way to collect GC (Garbage Collector) dumps of live .NET processes. It uses the EventPipe technology, which is a cross-platform alternative to ETW on Windows. GC dumps are created by triggering a GC in the target process, turning on special events, and regenerating the graph of object roots from the event stream. This process allows for GC dumps to be collected while the process is running and with minimal overhead."

Once you have a dump you can analyze it in Visual Studio or PerfView on GitHub.

PerfView

Sometimes you may capture a dump from one machine and analyze it on another. For that you may want to download the right symbols to debug your core dumps or minidumps. For that you'll use

dotnet-symbol

This is great for Linux debugging with lldb.

"Running dotnet-symbol against a dump file will, by default, download all the modules, symbols, and DAC/DBI files needed to debug the dump including the managed assemblies. Because SOS can now download symbols when needed, most Linux core dumps can be analyzed using lldb with only the host (dotnet) and debugging modules."

Interesting in some real tutorials on how to use these tools? Why not learn:

In the next blog post I'll look at dotnet trace and flame graphs!


Sponsor: Have you tried developing in Rider yet? This fast and feature-rich cross-platform IDE improves your code for .NET, ASP.NET, .NET Core, Xamarin, and Unity applications on Windows, Mac, and Linux.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
September 19, 2020 14:04
Great tutorial. It's so easy to do in .NET Core. That's one of the reasons I like it so much.
September 19, 2020 23:19
.NET Core is cross platform (sic) and open source (sic). Tell someone, maybe your boss. A good reminder. It's been this way for a half decade but I'm still bumping into folks who have never heard this.
So, you're asking us to do some pro bono advertisement for Microsoft? That entails putting our own reputations on the line. It becomes mortally dangerous when Microsoft has put the following on Channel 9:
For .NET Framework, you can only have one version of the framework installed on the same machine, which is used by all applications. ... This behavior might result in regressions. For developers, it usually means that the Framework upgrade was a company-wide decision performed by the IT department, which sometimes, delays the upgrades.
I watch this video on a machine that had several versions of .NET Framework installed side-by-side. And I'd rather my boss stayed away from that video, least he believes those technically erroneous assertions and give me a chashiering for not keeping him in the loop for "a company-wide decision" that "might result in regressions"! At least, do us a favor and spell "open-source" and "cross-platform" correctly. ("Open-source" is an adjective, while "open source" is a noun group with rare uses. "Cross platform" is just wrong.)
September 20, 2020 1:58
Fleet Command - You're talking about the Windows-only .NET Framework and your quoted item refers to that. The 4.x versions of the .NET Framework that Olya refers to does only allow one major version at a time. You may have 2 and 4 on your machine, but you don't have 4.6 and 4.7 etc on that same machine. .NET Core is in fact, cross platform, and runs anywhere, and you can have many versions of .NET Core installed AND if you like, an app can even have it's own private version.
September 20, 2020 18:13
Hi Scott, quick question: Do you know if there is any library I can add to my projects that give me the same information I get with counters? I usually have several stats reported to a grafana dashboard and I would love to have that information there too. Thanks in advance and regards,
September 22, 2020 0:36
@Marcelo Volmaro you don't require any external library to consume counters within your application. See this document on how to use the EventListener API to listen to counters in-process. You can use a library like prometheus-net-contrib to expose these metrics and then use a tool like prometheus/telegraf to collect this data and export to grafana. If you want to consume the counters from an external process (like dotnet-counters), you'll want to use the Microsoft.Diagnostics.NETCore.Client package to communicate with the diagnostics server. I'd recommend looking at the dotnet-counters source for how to do so.
September 23, 2020 21:31
NET Core is cross-plat form and open-source. Tell someone, maybe your boss. A good reminder. It's been this way for a half decade but I'm still bumping into folks who have never heard this. Apart from a subset of developers and IT reps, no one pays attention to new version releases. Business want the technology to work, be low cost, not have forced upgrades every two and a half years, be easy to deploy and easy to manage. Case in point, what is the business critical new feature which is going to be in .net 5? The list of improvements, once you exclude purely technical things such improved performance, unified code base, better cross-platoform, web assembly handling and such has little left to make a business case for upgrading to .net 5. Cost of upgrading multiple systems to .net 5 versus actual business improvements.
ST
September 24, 2020 21:43
@ST One of the great things about .net core and .net 5 is that you can bundle the framework with the tool that uses it. That is really freeing in our company for LOB apps as we are no longer dependent on getting .net updated on user's machines which can be a pain. The tools take up more hdd space, but that's not a big problem for us.
September 26, 2020 13:13
Hello! I had been having a really rough time lately and Alison has helped me out a lot. Even when I had not said anything for a while, she still made sure to check up on me to make sure I was ok, which I really appreciate. I feel very safe opening up to him and would propose Calmerry services to everyone who needed somebody to talk to!!!

(will show your gravatar icon)
2+5=?
Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, li, ol, pre, strike, strong, sub, super, u, ul) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.