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.