Obtain the call method name in. NET

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.