Obtain the call method name in ASP. NET

Source: Internet
Author: User

Obtain the call method name in ASP. NET

You need to add the corresponding namespace:

Copy the Code as follows:

Using System;

Using System. Diagnostics;

Using System. Reflection;

If you only get the current method name, you can use the following code:

Copy the Code as follows:

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 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 ();

}

But it is generally the caller who obtains the LOG method, so the following code is required: (this method is only for demonstration)

The Code is as follows:

Public static void WriteSysLog (string content)

{

Const int level = 1000;

StackTrace ss = new StackTrace (true );

// Index: 0 indicates its own method; 1 indicates the calling method; 2 indicates its upper layer; and so on.

MethodBase mb = ss. GetFrame (1). GetMethod ();

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 is available.

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 ();

}

What is interesting about this is that the Main caller

The Code is as follows:

System. AppDomain. _ nExecuteAssembly (Assembly assembly, String [] args)

Pass

The Code is as follows:

StackTrace ss = new StackTrace (true );

StackFrame [] sfs = ss. GetFrames ();

We can know the execution sequence of the. NET program:

The Code is as follows:

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)

Then go to 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.