You need to add the appropriate namespaces:
Copy code code as follows:
Using System;
Using System.Diagnostics;
Using System.Reflection;
If you are only getting the current method name, you can use the following code:
Copy code 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 namespaces
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 ();
}
Typically, however, you get the caller of this logging method, so you need to use the following code: (This method is demo only)
The code is as follows:
public static void Writesyslog (string content)
{
const int level = 1000;
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 ();
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, it's interesting that the caller of main
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 ();
can be known. NET Program Execution order:
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 enter the method main.
In addition, many other properties can be obtained from the MethodBase class and can be positioned to System.Reflection.MethodBase view.
Using reflection, you can iterate through all the property names, method names, and member names of the class, with one interesting little example: converting the value of a variable to the variable name itself by reflection.