繁体   English   中英

在 C# 中使用 Sentry 记录未处理的异常

[英]Logging unhandled exceptions with Sentry in C#

我正在编写一个 .NET 应用程序,它是 Autodesk Revit 程序的“插件”。 我想使用 Sentry 来记录和查看一般未处理的异常。 我了解,以下代码应捕获异常并将其发送到我的 sentry.io 帐户上的仪表板。

using (SentrySdk.Init("<my sentry dsn string>"))
{
    throw new DuplicateWaitObjectException();
}

但是,我的仪表板上没有显示 DuplicateWaitObjectException。 我不知道为什么上面的代码不起作用,特别是因为明确地捕获错误可以正常工作:

SentrySdk.Init("<my sentry dsn string>");

try
{
    throw new DuplicateWaitObjectException();
}
catch (Exception err)
{
    SentrySdk.CaptureException(err);
}

我怀疑这可能与我的应用程序是 Revit 程序加载的插件有关,而不是独立的应用程序,但我真的不确定。 我相信我的代码(第一个块)应该有效吗?

我正在考虑缓解这个问题,而不是将我的代码包装在 Revit 入口点的 try-catch 块中,但这对我来说似乎很棘手,因为我不知道是什么导致了这个问题。

我在 Visual Studio 2019 中为 Windows (10) 工作并为 Revit 2019 开发。我的 SentrySDK 版本是 v2.1.1。

谢谢阅读

Revit 会捕获任何未处理的异常,因此该库将无法看到它们。

您将代码包装在 try-catch 块中的想法是迄今为止最简单的解决方案。
唯一的另一种选择是在单独的 AppDomain 或进程中运行代码,并使用远程处理或 gRPC 之类的东西与 Revit API 进行通信。

我认为上面提供的代码是最好的解决方案。 https://stackoverflow.com/a/54698824/6247420中提供了相同的想法

[Transaction(TransactionMode.ReadOnly)]
    public class MyCommand: IExternalCommand 
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementS

    et elements) {

    SentrySdk.Init("<my sentry dsn string>");
            try 
            {
                //do stuff
            } catch (Exception exp) {
               SentrySdk.CaptureException(exp);
            }
        } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM