.NET method name gets called in

In writing logging function, you need to record the name of the module where the caller log, namespace names, class names and method names, think of using the reflection (Please note that involve reflection performance), but do not know which specific together , so the search, are summarized as follows:

We need to add the appropriate namespace:

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
public static void WriteSysLog(int level, string content)
{
    MethodBase mb = MethodBase.GetCurrentMethod();
    string systemModule = Environment.NewLine;
    systemModule += "模块名:" + mb.Module.ToString() + Environment.NewLine;
    systemModule + = "namespace name:" + mb.ReflectedType.Namespace + Environment.NewLine;
    // fully qualified name, including namespace
    systemModule += "类名:" + 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();
}
Copy the code

This article addresses: http://www.cnblogs.com/Interkey/p/GetMethodName.html
but under normal circumstances is to get this logging method of the caller, so need to use the following code demonstrates this method only :()

Copy the code
public static void WriteSysLog(string content)
{
    const int level = 1000;

    StackTrace ss = new StackTrace(true);
    // index: 0 as the method itself; 1 invokes the method; its upper layer 2, and so on
    MethodBase mb = ss.GetFrame(1).GetMethod();

    // This article addresses: http: //www.cnblogs.com/Interkey/p/GetMethodName.html
    StackFrame[] sfs = ss.GetFrames();
    string systemModule = Environment.NewLine;
    systemModule += "模块名:" + mb.Module.ToString() + Environment.NewLine;
    systemModule + = "namespace name:" + mb.DeclaringType.Namespace + Environment.NewLine;
    // class name only
    systemModule += "类名:" + 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();
}
Copy the code

For a little, feeling is interesting that the caller Main, System.AppDomain._nExecuteAssembly (Assembly assembly, String [] args).

by

StackTrace ss = new StackTrace(true);
StackFrame[] sfs = ss.GetFrames();

.NET program that can execute the order of:

  1. System.Threading.ThreadHelper.ThreadStart()
  2. System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  3. Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  4. System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)

Then enter the Main method.

 

Further, from  MethodBase class  may also acquire a number of other properties, may be positioned to System.Reflection.MethodBase own view.

Use reflection can traverse all stats by class name, method name, members, of which an interesting little example: by reflecting the value of the variable into the variable name itself 

 

REFERENCE FROM : http://www.cnblogs.com/Interkey/p/GetMethodName.html

Reproduced in: https: //www.cnblogs.com/zhangchenliang/p/4134564.html

Guess you like

Origin blog.csdn.net/weixin_33995481/article/details/93495475