Obtain the call method name in. NET
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, I do not know which one is, so I need to search and sort it out as follows: I 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; // fully qualified 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: http://www.cnblogs.com/Interkey/p/GetMethodName.html But it is generally the caller who obtains the LOG method, so the following code is required: (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 indicates its own method; 1 indicates the call method; 2 indicates 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; systemModule + = "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. _ nExecuteAssembly (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.