How to get line number(s) in the StackTrace of an exception thrown in .NET to show up

后端 未结 6 1609
醉梦人生
醉梦人生 2020-12-04 21:57

MSDN says this about the StackTrace property of the Exception class:

The StackTrace property holds a stack trace, which yo

6条回答
  •  醉梦人生
    2020-12-04 22:32

    You could try the following, given there is a pdb file for the assembly:

    try
    {
        throw new Exception();
    }
    catch (Exception ex)
    {
        // Get line number from the stack trace's top frame for the exception with source file information
        int linenumber = (new StackTrace(ex, true)).GetFrame(0).GetFileLineNumber();
    }
    

提交回复
热议问题