How can I get dotnet to show shorter stack traces?

1k Views Asked by At

I'm running .NET Core 3.1 on macOS, using the dotnet watch command line to run my web application and unit tests. When my app crashes, the stack trace is frequently so large that I can't see what actually went wrong:

dotnet watch error with large stack trace

Is there some way I can get the dotnet command to only show the stack frames from my own code (as opposed to the .NET Core / Entity Framework calls), so I get an error something more like:

Unhandled exception. System.InvalidOperationException: The entity type 
   'Attendee' requires a primary key to be defined. If you intended 
   to use a keyless entity type call 'HasNoKey()'.
   [...50 non-user stack frames hidden...]
   at Website.Program.Main(String[] args) in /code/my-app/Program.cs:line 7
watch : Exited with error code 134
watch : Waiting for a file to change before restarting dotnet...

Failing that - any way to just show, say, the first five lines of the stack trace?

1

There are 1 best solutions below

1
On

Maybe could be useful just to split Environment.StackTrace string and get several lines from the top since the most recent calls go first.

string[] stackTraceRows = Environment.StackTrace.Split('\n');
int rowCount = 3; // for example, to display 3 lines
for(int i=0; i<rowCount; i++){
    Console.WriteLine(stackTraceRows[i]);
}