获取当前类得位置以及方法名
StackTrace st = new StackTrace(new StackFrame(true));Console.WriteLine(" Stack trace for current level: {0}", st.ToString());StackFrame sf = st.GetFrame(0);Console.WriteLine(" File: {0}", sf.GetFileName());Console.WriteLine(" Method: {0}", sf.GetMethod().Name);Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber());Console.WriteLine(" Column Number: {0}", sf.GetFileColumnNumber());
例子:
StackFrame sf = new StackTrace(new StackFrame(true)).GetFrame(0);Debug.WriteLine(string.Format("错误: 方法[{1}.{2}({0})] 报错: {3} ;", finalBlog.Id, sf.GetFileName(), sf.GetMethod().Name, ex.ToString()));
输出:
错误: 方法[E:\SNS\Social\Code\SNS_DCC\BlogOperate.cs.DelSNSBlogToMongo(0)] 报错: System.IndexOutOfRangeException: 索引超出了数组界限。