Print the source filename and linenumber in C#

后端 未结 5 1946
盖世英雄少女心
盖世英雄少女心 2020-11-29 23:24

Is there any way to retrieve the current source filename and linenumber in C# code and print that value in the console output? Like LINE and FILE

5条回答
  •  囚心锁ツ
    2020-11-29 23:30

    If you want some more internal detail, but you don't specifically need filename and line number, you can do something like this:

    System.Diagnostics.Debug.Print(this.GetType().ToString() + " My Message");
    

    This has an advantage over printing out the filename in that if you put this in a parent class, it will print out the child class name that is actually running the code.

提交回复
热议问题