码迷,mamicode.com
首页 > Windows程序 > 详细

C#获取当前运行的源代码的文件名和当前源代码的行数的方法

时间:2015-09-09 19:04:46      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

在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();

        }

转自:http://blog.csdn.net/weizhiai12/article/details/7062854

C#获取当前运行的源代码的文件名和当前源代码的行数的方法

标签:

原文地址:http://www.cnblogs.com/iZander/p/4795399.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!