4

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?

magicandre1981
  • 27,895
  • 5
  • 86
  • 127
Dylan Beattie
  • 53,688
  • 35
  • 128
  • 197
  • 2
    Please, have a look at [Ben.Demystifier](https://github.com/benaadams/Ben.Demystifier) tool, it makes stack traces more productive – Pavel Anikhouski Apr 07 '20 at 12:08
  • The [StackTrace class](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stacktrace.-ctor?view=netframework-4.8) has some options. I don't think it has the capability to show just your code, but you could probably make that happen with some reflection. At minimum you can choose how many frames you want. – Crowcoder Apr 07 '20 at 12:15
  • 1
    I don't think there's any way to convince dotnet watch to change this behavior, but if you write your application so that you're catching and logging errors instead of relying on dotnet to do that for you, you can have some control over how they get output. – StriplingWarrior Apr 07 '20 at 14:50
  • 1
    Does this answer your question? [StackTrace: how to show only my code?](https://stackoverflow.com/questions/6041953/stacktrace-how-to-show-only-my-code) – Martin Apr 08 '20 at 23:41
  • @Martin I'm afraid not... I'm not actually writing any .NET code here that works with the exceptions and stack traces; my question was about the dotnet CLI tooling and whether there was any option to filter the default output. This is for some workshop examples I'm creating around EF Core, and I don't want to include a lot of extra exception handling & filtering code. The linked answer is useful, but not directly relevant - sorry! – Dylan Beattie Apr 25 '20 at 14:31

0 Answers0