As the C# compiler gets smarter and smarter and more complex features like iterators, generics, async/await are added, our stack traces become more and more unreadible. This is because the .NET stack traces output the compiler transformed methods; rather than the source code methods, which make them slow to mentally parse and match back to the source code.
A library that can help you get more readible stacktraces is Ben.Demystifier.
Let’s try it:
- Create a new console application. Set the C# target version to 7.1(Instructions here)
- Include the Ben.Demystifier nuget package
- Add the following code:
- Note that we call the exception.Demystify() extension method to generate a better stacktrace.
- Let’s now run the application first and compare our stacktraces:
The normal stacktrace(with a lot of compiler noise):
at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at ConsoleApp1.Program.<Iterator>d__2.MoveNext() in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 35
at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
at ConsoleApp1.Program.<AsyncCount>d__1.MoveNext() in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 28
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at ConsoleApp1.Program.<Main>d__0.MoveNext() in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 15
The demystified stacktrace:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at bool System.Collections.Generic.List<T>+Enumerator.MoveNextRare()
at IEnumerable<string> ConsoleApp1.Program.Iterator(int startAt)+MoveNext() in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 35
at int System.Linq.Enumerable.Count<TSource>(IEnumerable<TSource> source)
at async Task ConsoleApp1.Program.AsyncCount() in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 28
at async Task ConsoleApp1.Program.Main(string[] args) in c:\projects\test\ConsoleApp1\ConsoleApp1\Program.cs:line 15
Check out the documentation to see the full list of problems with your default stacktraces that this library solves.