How can I get the line number which threw exception?

后端 未结 12 2179
既然无缘
既然无缘 2020-11-27 09:14

In a catch block, how can I get the line number which threw an exception?

12条回答
  •  温柔的废话
    2020-11-27 10:17

    I added an extension to Exception which returns the line, column, method, filename and message:

    public static class Extensions
    {
        public static string ExceptionInfo(this Exception exception)
        {
    
            StackFrame stackFrame = (new StackTrace(exception, true)).GetFrame(0);
            return string.Format("At line {0} column {1} in {2}: {3} {4}{3}{5}  ",
               stackFrame.GetFileLineNumber(), stackFrame.GetFileColumnNumber(),
               stackFrame.GetMethod(), Environment.NewLine, stackFrame.GetFileName(),
               exception.Message);
    
        }
    }
    

提交回复
热议问题