在C#中记录日志时,为了以后查找错误或者跟踪的方便,最好能记录下出错的源代码的文件名和出错的源代码的行数。

这2个方法如下:

/// <summary>
        /// 取得当前源码的哪一行
        /// </summary>
        /// <returns></returns>
        public static int GetLineNum()
        {
            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
            return st.GetFrame(0).GetFileLineNumber();
        }

        /// <summary>
        /// 取当前源码的源文件名
        /// </summary>
        /// <returns></returns>
        public static string GetCurSourceFileName()
        {
            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);

            return st.GetFrame(0).GetFileName();

        }



相关文章:

  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
  • 2021-04-22
  • 2022-02-24
  • 2022-12-23
  • 2021-11-29
  • 2021-06-17
猜你喜欢
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
  • 2022-02-26
  • 2021-08-06
相关资源
相似解决方案