在寫記錄日志功能時,需要記錄日志調用方所在的模塊名、命名空間名、類名以及方法名,想到使用的是反射(涉及到反射請注意性能),但具體是哪一塊兒還不了解,於是搜索,整理如下:
需要添加相應的命名空間:
using System; using System.Diagnostics; using System.Reflection;
如果僅是獲取當前方法名使用 MethodBase.GetCurrentMethod 方法(返回表示當前正在執行的方法的 MethodBase 對象),可以使用如下代碼:
public static void WriteSysLog(int level, string content) { MethodBase mb = MethodBase.GetCurrentMethod(); string systemModule = Environment.NewLine; systemModule += "模塊名:" + mb.Module.ToString() + Environment.NewLine; systemModule += "命名空間名:" + mb.ReflectedType.Namespace + Environment.NewLine; //完全限定名,包括命名空間 systemModule += "類名:" + mb.ReflectedType.FullName + Environment.NewLine; systemModule += "方法名:" + mb.Name; Console.WriteLine("LogDate: {0}{1}Level: {2}{1}systemModule: {3}{1}content: {4}", DateTime.Now, Environment.NewLine, level, systemModule, content); Console.WriteLine(); }
本文地址:http://www.cnblogs.com/Interkey/p/GetMethodName.html
但一般情況下是獲取此記錄日志方法的調用方,因此需要使用下面的代碼:(此方法僅為演示)
public static void WriteSysLog(string content) { const int level = 1000; StackTrace ss = new StackTrace(true); //index:0為本身的方法;1為調用方法;2為其上上層,依次類推 MethodBase mb = ss.GetFrame(1).GetMethod(); //本文地址:http://www.cnblogs.com/Interkey/p/GetMethodName.html StackFrame[] sfs = ss.GetFrames(); string systemModule = Environment.NewLine; systemModule += "模塊名:" + mb.Module.ToString() + Environment.NewLine; systemModule += "命名空間名:" + mb.DeclaringType.Namespace + Environment.NewLine; //僅有類名 systemModule += "類名:" + mb.DeclaringType.Name + Environment.NewLine; systemModule += "方法名:" + mb.Name; Console.WriteLine("LogDate: {0}{1}Level: {2}{1}systemModule: {3}{1}content: {4}", DateTime.Now, Environment.NewLine, level, systemModule, content); Console.WriteLine(); }
對於這一點兒,感覺有意思的是Main的調用方,System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)。
通過
StackTrace ss = new StackTrace(true); StackFrame[] sfs = ss.GetFrames();
可以得知.NET程序的執行順序:
- System.Threading.ThreadHelper.ThreadStart()
- System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
- Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
- System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
然后進入方法Main中。
StackFrame指的是一個.net運行的時候堆棧上的一個幀(Frame),每次進入一個方法的時候就會有一個新的方法幀壓入線程執行堆棧,可以通過StackFrame獲取相關的信息,比如當前代碼所在文件名和行號。
拓展方法是在.NET Framework 3.5以上版本才支持的,需要組件System.Core。
相關類:
- 在System.Diagnostics 命名空間中的 StackTrace 類 ,表示一個堆棧跟蹤,它是一個或多個堆棧幀的有序集合。
- 在System.Diagnostics 命名空間中的 StackFrame 類 ,提供關於 StackFrame(表示當前線程的調用堆棧中的一個函數調用)的信息。
- 在System.Reflection 命名空間中的 MethodBase 類 ,提供有關方法和構造函數的信息。
另外,從 MethodBase 類 還可以獲取很多其他屬性,可以自行定位到System.Reflection.MethodBase 查看。
使用反射可以遍歷獲得類的所有屬性名,方法名,成員名,其中一個有趣的小例子:通過反射將變量值轉為變量名本身
文件:GetCurrentMethodName.zip [VS版本為2013,本例僅為演示]
后續:
感謝博友的提醒,使用CallerMemberNameAttribute 也可以實現獲取調用方法名。示例請參考:調用方信息(C# 和 Visual Basic)。
C# 5.0 給我們帶來了三個非常有用的編譯器特性:CallerMemberNameAttribute、CallerFilePathAttribute、CallerLineNumberAttribute (在System.Runtime.CompilerServices 命名空間中),但這三個Attribute 是定義.NET Framework 4.5里的。
如果在低於.NET4.5中使用需要定義這些特性:
namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] public class CallerMemberNameAttribute : Attribute { } [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] public class CallerFilePathAttribute : Attribute { } [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] public class CallerLineNumberAttribute : Attribute { } }
關於 Attribute 和 AttributeUsageAttribute 的使用可自行搜索,有些偏題了…
參考: