Get the call method name in. NET and. net get the call Method
When writing logs, you must record the module name, namespace name, class name, And Method Name of the log caller. Reflection is used (when reflection is involved, pay attention to performance ), however, the search is as follows:
You need to add the corresponding namespace: using System; using System. diagnostics; using System. reflection; if you only get the current method name, you can use the following code: copy the public static void WriteSysLog (int level, string content) {MethodBase mb = MethodBase. getCurrentMethod (); string systemModule = Environment. newLine; systemModule + = "module name:" + mb. module. toString () + Environment. newLine; systemModule + = "namespace name:" + mb. reflectedType. namespace + Environment. newLine; // full limit Name, including the namespace systemModule + = "Class Name:" + mb. reflectedType. fullName + Environment. newLine; systemModule + = "method name:" + mb. name; Console. writeLine ("LogDate: {0} {1} Level: {2} {1} systemModule: {3} {1} content: {4}", DateTime. now, Environment. newLine, level, systemModule, content); Console. writeLine ();} copy the code this article address: publish (this method is only for demonstration) copy the code public Static void WriteSysLog (string content) {const int level = 1000; StackTrace ss = new StackTrace (true); // index: 0 is its own method; 1 is the call method; 2 is its upper layer, and so on MethodBase mb = ss. getFrame (1 ). getMethod (); // address of this article: http://www.cnblogs.com/Interkey/p/GetMethodName.html StackFrame [] sfs = ss. getFrames (); string systemModule = Environment. newLine; systemModule + = "module name:" + mb. module. toString () + Environment. newLine; systemModu Le + = "namespace name:" + mb. declaringType. namespace + Environment. newLine; // only the class name systemModule + = "Class Name:" + mb. declaringType. name + Environment. newLine; systemModule + = "method name:" + mb. name; Console. writeLine ("LogDate: {0} {1} Level: {2} {1} systemModule: {3} {1} content: {4}", DateTime. now, Environment. newLine, level, systemModule, content); Console. writeLine ();} copy the code. appDomain. _ nExecuteA Ssembly (Assembly assembly, String [] args ). Use StackTrace ss = new StackTrace (true); StackFrame [] sfs = ss. getFrames. NET program execution sequence: System. threading. threadHelper. threadStart () System. threading. executionContext. run (ExecutionContext executionContext, ContextCallback callback, Object state) Microsoft. visual Studio. hostingProcess. hostProc. runUsersAssembly () System. appDomain. _ nExecuteAssembly (Assembly assembly, String [] args) then enters the Main method. In addition, you can obtain many other attributes from the MethodBase class and locate System. Reflection. MethodBase on your own. Reflection can be used to retrieve all the attribute names, method names, and member names of the class. An interesting small example: Convert the variable value to the variable name itself through reflection.