首页 > 解决方案 > 如何在 .NET core 或 .NET 5 上获取本机异常

问题描述

发生崩溃时我需要获取调用堆栈。

我们的项目使用 C++/CLI 来组合 C# 和 C++ 代码。

来自 C# 和 C++/CLI 的异常,我们可以使用 try-catch 来获取异常。

但是来自本地 C++ 的异常,尤其是被认为不可恢复的异常,或者发生在另一个线程中的异常,例如AccessViolationException, StackOverflowException.

int* p = nullptr;
*p = 123;

而且我还看到当本机异常发生时,控制台将打印此消息,然后应用程序崩溃:

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
   at <Module>.NativeCppDemo.MyNativeCppTestClass.memberFunctionAddFunCrash(NativeCppDemo.MyNativeCppTestClass*, Int32, Int32)
--------------------------------
   at ClrCrashTestNetCore.ClrTest.memberFunAddCrash(Int32, Int32, Boolean)
   at CsCrashDemoNetCore.Program.Run()
   at CsCrashDemoNetCore.Program.Main(System.String[])

这是我想要的消息,但我不知道如何在代码中获取它。当异常发生在另一个线程上时,我发现了此消息,它不会在控制台上打印。

我们有什么方法可以从原生 C++ 中获取异常信息吗?喜欢有一些接口来获取异常吗?或者使用 profiler 可以获得调用堆栈?

标签: c#c++.net-coreclrcoreclr

解决方案


推荐阅读