今天热门
热点:

记录错误日志的时候怎么获取当前的文件和行数,日志行数


比如在C++中有__FILE__和__LINE__用来记录当前的文件和当前行数,不知道C#中是否有类似的东西记录当前的文件和当前的行数,如果还有方法能取出当前的函数是什么就更好了,谢谢

解决方案

StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(0);
Console.WriteLine("      {0}", sf.GetMethod());

  
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 

最新版本:20070212

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html

try
{
using (StreamReader sr = new StreamReader("txt.txt"))
{
String line;
int i = 0;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine("第{0}行", i);
i++;
}
}
}
catch (Exception e)
{
}

using System;
using System.Diagnostics;

class StackTraceSample
{
    [STAThread]
    static void Main(string[] args)
    {
        StackTraceSample sample = new StackTraceSample();
        try
        {
            sample.MyPublicMethod();
        }
        catch (Exception)
        {
            // Create a StackTrace that captures
            // filename, line number, and column
            // information for the current thread.
            StackTrace st = new StackTrace(true);
            for(int i =0; i< st.FrameCount; i++ )
            {
                // Note that high up the call stack, there is only
                // one stack frame.
                StackFrame sf = st.GetFrame(i);
                Console.WriteLine();
                Console.WriteLine("High up the call stack, Method: {0}",
                    sf.GetMethod());

                Console.WriteLine("High up the call stack, Line Number: {0}",
                    sf.GetFileLineNumber());
            }
        }
    }

    public void MyPublicMethod () 
    { 
        MyProtectedMethod(); 
    }

    protected void MyProtectedMethod ()
    {
        MyInternalClass mic = new MyInternalClass();
        mic.ThrowsException();
    }

    class MyInternalClass
    {
        public void ThrowsException()
        {
            try
            {
                throw new Exception("A problem was encountered.");
            }
            catch (Exception e)
            {
                // Create a StackTrace that captures filename,
                // line number and column information.
                StackTrace st = new StackTrace(true);
                string stackIndent = "";
                for(int i =0; i< st.FrameCount; i++ )
                {
                    // Note that at this level, there are four
                    // stack frames, one for each method invocation.
                    StackFrame sf = st.GetFrame(i);
                    Console.WriteLine();
                    Console.WriteLine(stackIndent + " Method: {0}",
                        sf.GetMethod() );
                    Console.WriteLine(  stackIndent + " File: {0}", 
                        sf.GetFileName());
                    Console.WriteLine(  stackIndent + " Line Number: {0}",
                        sf.GetFileLineNumber());
                    stackIndent += "  ";
                }
                throw e;
            }
        }
    }
}

new   System.Diagnostics.StackFrame(true).GetFileLineNumber().ToString()   
  new   System.Diagnostics.StackFrame(true).GetFileName().ToString();

www.zrccd.nettrue/topics/20230304/774378.htmlTechArticle记录错误日志的时候怎么获取当前的文件和行数,日志行数 比如在C++中有__FILE__和__LINE__用来记录当前的文件和当前行数,不知道C#中是否有类似的东西记录当前的文件和当前的行数,如果...

相关文章

    暂无相关文章

用户评论

大家都在看