【问题标题】:Logging unhandled exceptions with Sentry in C#在 C# 中使用 Sentry 记录未处理的异常
【发布时间】:2020-09-01 12:30:06
【问题描述】:

我正在编写一个 .NET 应用程序,它是 Autodesk Revit 程序的“插件”。我想使用 Sentry 来记录和查看一般未处理的异常。 From my understanding,以下代码应捕获异常并将其发送到我的 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 for Windows (10) 并为 Revit 2019 进行开发。我的 SentrySDK 版本是 v2.1.1。

感谢阅读

【问题讨论】:

    标签: c# exception .net-core sentry revit-api


    【解决方案1】:

    我认为上面提供的代码是最好的解决方案。同样的想法在 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);
                }
            } 
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2013-12-15
        • 1970-01-01
        • 2015-04-10
        • 2017-10-08
        • 1970-01-01
        • 1970-01-01
        • 2020-11-26
        • 2015-12-21
        相关资源
        最近更新 更多