How to get error line number of code using try-catch

前端 未结 10 958
轮回少年
轮回少年 2020-12-05 13:08

I want to get line number of code which cause error. For example;

static void Main(string[] args)
{
    using (SqlConnection conn = new SqlConnection(bagcum)         


        
10条回答
  •  孤城傲影
    2020-12-05 13:51

    You might get 0 in result if you don't initialize StackTrace to include fileinfo.

    enter image description here

    Try this

    try
    {
        //code
    }
    catch (Exception e)
    {
        var lineNumber = new System.Diagnostics.StackTrace(e, true).GetFrame(0).GetFileLineNumber();
    }
    

    This worked for me.

提交回复
热议问题