c# - 如何在 .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 可以获得调用堆栈?
解决方案
推荐阅读
- python - 从完整句子中解析日期
- c - fscanf read()s 超过了我要求的字符数
- javascript - 为什么 () => this 不返回封闭对象?
- python - 使用作为参数发送的颜色绘制矩形时出错 OpenCV Python
- python - 无法在 python 中创建 MQSQL 数据库(cursor = db.cursor() 不起作用) - 新手问题
- c - (void *) 的 realloc 中的内存泄漏以连接字符串
- c - FFMpeg、libav、sws_scale 和 yuv 到 rgb 转换的问题 - 颜色不正确
- python - 计算csv中特定列中具有相同值的行
- c# - 如何在 Xamarin 中创建可以通过加速度计更改其位置的视图
- javascript - 将组件放在 React 中深度嵌套的子元素之上