BT

Cleaner Stack Traces in .NET/.NET Core

| by Jonathan Allen Follow 639 Followers on Jan 29, 2018. Estimated reading time: 1 minute |

In exception-based languages, stack traces are one of the most important tools for diagnosing problems. In some cases, a brief error message and the stack trace is the only information a developer has available, especially when Personably Identifiable Information (PII) constraints limit what can be logged.

With the advent of the Task Parallel Library (TPL), and later async/await, stack traces have been getting increasingly difficult to understand. It is not unusual for the useful information to be hidden among multiple occurrences of:

And this is actually an improvement. Prior to .NET 4.5 and introduction of the ExceptionDispatchInfo class, stack traces couldn’t cross async boundaries and developers would instead have to dig through inner exceptions. But more can be done.

Illyriad games developer Ben Adams has taken it upon himself to improve the stack traces in .NET Core 2.1. His work focused on these areas:

  1. Remove the noise introduced by async/await call points.
  2. Show which method overload was being called when in an async or iterator context.

Meanwhile, Anirudh Agnihotry updated the KeyNotFoundException used by the Dictionary/ConcurrentDictionary to indicate which key wasn’t found, an annoyance for .NET developers going back to version 1.0.

If you are on .NET Framework or don’t want to wait for .NET Core 2.1, you can still take advantage of Ben’s work by using the Ben.Demystifier library. This goes beyond what his .NET Core changes did and makes the stack trace read much closer to the original C# code. (This wasn’t an option for .NET Core, as showing C# keywords in a VB or F# stack trace wouldn’t be helpful.)

.NET Core is published with the MIT license. Ben.Demystifier is available under the Apache license.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss
BT