【问题标题】:How to get line number(s) in the StackTrace of an exception thrown in .NET 6 on Linux如何在 Linux 上的 .NET 6 中抛出异常的 StackTrace 中获取行号
【发布时间】:2022-01-17 09:23:23
【问题描述】:

我在 .NET 6 Framework 下编译(我也试过 .NET Core 3.1)Visual Studio 中的 C# 代码

虽然它在 Windows 下运行良好,但使用相同的选项,我无法在 Linux (Ubuntu 18.04) 下获取异常的 StackTrace 中的行号。

  • 存在 pdb 文件
  • 在 Linux 机器上安装了 .NET 6
  • 部署模式:自包含

【问题讨论】:

  • 你是用 Debug 还是 Release 配置编译的?
  • 发布,但我也尝试调试以进行测试,但没有帮助。

标签: c# .net linux exception stack-trace


【解决方案1】:

试试这个方法。我是在需要出错的行号时写的。

''' <summary>
''' 
''' </summary>
''' <param name="ex"></param>
Public Sub ErrorShow(ByVal ex As Exception)
    Dim Result As String
    Dim hr As Integer = Runtime.InteropServices.Marshal.GetHRForException(ex)
    Result = ex.GetType.ToString & "(0x" & hr.ToString("X8") & "): " & ex.Message & Environment.NewLine & ex.StackTrace & Environment.NewLine
    Dim st As StackTrace = New StackTrace(ex, True)
    For Each sf As StackFrame In st.GetFrames
        If sf.GetFileLineNumber() > 0 Then
            Result &= "Line:" & sf.GetFileLineNumber() & " Filename: " & IO.Path.GetFileName(sf.GetFileName) & Environment.NewLine
        End If
    Next
    Console.WriteLine(Result)
End Sub

【讨论】:

    猜你喜欢
    • 2011-04-17
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 2021-08-03
    • 2016-09-02
    • 1970-01-01
    • 2011-09-07
    • 2010-12-08
    相关资源
    最近更新 更多