开发过程中,我们有时候需要记录一下调用信息
1 public static void TraceMessage(string message, 2 [CallerMemberName] string memberName="", 3 [CallerFilePath] string sourceFilePath="", 4 [CallerLineNumber] int sourceLineNumber=0) 5 { 6 Console.WriteLine($"message:{message}/nmember name: {memberName}" + 7 $"/nsource file path: {sourceFilePath}/nsource line number: {sourceLineNumber}"); 8 } 9 10 11 public void DoSomething() 12 { 13 TraceMessage("事情开始起变化!"); 14 }
调用DoSomething的结果如下
message:事情开始起变化! member name: DoSomething //调用函数(准确讲应该是成员)名称 source file path: /Users/qinyuanlong/DotNet_core_x1/CallerMemberNameConsole/CallerMemberNameConsole/Program.cs //调用文件 source line number: 16 //调用行号
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/281887.html