簡體   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