如何让 dotnet 显示较短的堆栈跟踪?

问题描述 投票:0回答:1

我在 macOS 上运行 .NET Core 3.1,使用

dotnet watch
命令行来运行我的 Web 应用程序和单元测试。当我的应用程序崩溃时,堆栈跟踪通常非常大,以至于我无法看到到底出了什么问题:

有什么方法可以让

dotnet
命令仅显示我自己的代码中的堆栈帧(而不是 .NET Core / Entity Framework 调用),所以我收到的错误更像是:

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...

如果失败 - 有什么方法可以只显示堆栈跟踪的前五行吗?

c# .net-core stack-trace dotnet-watch
1个回答
0
投票

也许只需要分割

Environment.StackTrace
字符串并从顶部获取几行,因为最近的调用先进行。

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]);
}
© www.soinside.com 2019 - 2024. All rights reserved.