Asp. NET implementation gets the calling method name

Source: Internet
Author: User
Tags datetime reflection tostring

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.

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.