When writing the logging function, you need to record the log caller's module name, namespace name, class name, and method name, think of the use of reflection (please note the performance of the reflection), but specifically what is not understood, so search, organized as follows:
You need to add the appropriate namespaces:
Using system;using system.diagnostics;using System.Reflection;
If you are only getting the current method name, you can use the following code:
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 ();}
This address: http://www.cnblogs.com/Interkey/p/GetMethodName.html
In general, however, you get the caller of this logging method, so you need to use the following code: (This method is for demonstration only)
public static void Writesyslog (string content) { Const int. level = +; StackTrace ss = new StackTrace (true); Index:0 is the method for itself, 1 is the calling method, 2 is the upper layer, and so on methodbase MB = ss. GetFrame (1). GetMethod (); This article address: 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 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 ();}
For this, the interesting thing is the caller of Main, system.appdomain._nexecuteassembly (Assembly Assembly, string[] args).
Pass
StackTrace ss = new StackTrace (true); stackframe[] SFS = ss. Getframes ();
Can know. NET Program Execution order:
- 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 enter the method main.
In addition, many other properties can be obtained from the MethodBase class, and you can navigate to System.Reflection.MethodBase view yourself.
Using reflection, you can iterate through all the property names of the class, the method name, the member name, and one interesting little example: converting the value of a variable to the variable name itself by reflection
REFERENCE from:http://www.cnblogs.com/interkey/p/getmethodname.html
Get call Method name in. NET