现在的位置: 首页 > 综合 > 正文

张芳小畔子

2016年10月16日 ⁄ 综合 ⁄ 共 3225字 ⁄ 字号 评论关闭

  在写记录日志功能时,需要记录日志调用方所在的模块名、命名空间名、类名以及方法名,想到使用的是反射(涉及到反射请注意性能),但具体是哪一块儿还不了解,于是搜索,整理如下:

需要添加相应的命名空间:

using System;
using System.Diagnostics;
using System.Reflection;

如果仅是获取当前方法名使用 MethodBase.GetCurrentMethod 方法(返回表示当前正在执行的方法的 MethodBase 对象),可以使用如下代码:

复制代码
public static void WriteSysLog(int level, string content)
{
    MethodBase mb = MethodBase.GetCurrentMethod();
    string systemModule = Environment.NewLine;
    systemModule += "模块名:" + mb.Module.ToString() + Environment.NewLine;
    systemModule += "命名空间名:" + mb.ReflectedType.Namespace + Environment.NewLine;
    //完全限定名,包括命名空间
    systemModule += "类名:" + mb.ReflectedType.FullName + Environment.NewLine;
    systemModule += "方法名:" + mb.Name;

    Console.WriteLine("LogDate: {0}{1}Level: {2}{1}systemModule: {3}{1}content: {4}", DateTime.Now, Environment.NewLine, level, systemModule, content);
    Console.WriteLine();
}
复制代码

本文地址:http://www.cnblogs.com/Interkey/p/GetMethodName.html
但一般情况下是获取此记录日志方法的调用方,因此需要使用下面的代码:(此方法仅为演示)

复制代码
public static void WriteSysLog(string content)
{
    const int level = 1000;

    StackTrace ss = new StackTrace(true);
    //index:0为本身的方法;1为调用方法;2为其上上层,依次类推
    MethodBase mb = ss.GetFrame(1).GetMethod();

    //本文地址:http://www.cnblogs.com/Interkey/p/GetMethodName.html
    StackFrame[] sfs = ss.GetFrames();
    string systemModule = Environment.NewLine;
    systemModule += "模块名:" + mb.Module.ToString() + Environment.NewLine;
    systemModule += "命名空间名:" + mb.DeclaringType.Namespace + Environment.NewLine;
    //仅有类名
    systemModule += "类名:" + mb.DeclaringType.Name + Environment.NewLine;
    systemModule += "方法名:" + mb.Name;

    Console.WriteLine("LogDate: {0}{1}Level: {2}{1}systemModule: {3}{1}content: {4}", DateTime.Now, Environment.NewLine, level, systemModule, content);
    Console.WriteLine();
}
复制代码

对于这一点儿,感觉有意思的是Main的调用方,System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)。

通过

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

可以得知.NET程序的执行顺序:

  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)

然后进入方法Main中。 

 

相关类:

  1. 在System.Diagnostics 命名空间中的 StackTrace
     ,表示一个堆栈跟踪,它是一个或多个堆栈帧的有序集合。
  2. 在System.Diagnostics 命名空间中的 StackFrame
     ,提供关于 StackFrame(表示当前线程的调用堆栈中的一个函数调用)的信息。
  3. 在System.Reflection 命名空间中的 MethodBase
     ,提供有关方法和构造函数的信息。

另外,从 MethodBase 类 还可以获取很多其他属性,可以自行定位到System.Reflection.MethodBase
查看。

使用反射可以遍历获得类的所有属性名,方法名,成员名,其中一个有趣的小例子:通过反射将变量值转为变量名本身 

文件:GetCurrentMethodName.zip[VS版本为2013,本例仅为演示]


 

参考:

  1. Logging method name in .NET
  2. c# 获取命名空间 类名 方法名
  3. 得到当前正在运行的方法或属性名[C#] 
  4. C#如何获取被调用的方法的名称及参数列表?
  5. 获得当前程序的 空间名.类名.方法名
  6. C#获得调用方法的名称和类名


作者:Cosmic_Spy 
出处:http://www.cnblogs.com/Interkey/ 
有兴趣可以加群【.NET破解|反编译】请注明:博客园,谢谢~ 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

分类: .netC#
winform
skills随笔
2
0
(请您对文章做出评价)
« 上一篇:使用DataConnectionDialog在运行时设置数据源连接字符串

posted on 2014-11-30 23:54 Cosmic_Spy 阅读(1211) 评论(4编辑 收藏

评论

#1楼 2014-12-01
08:59
kingcomxu  

试试CallerMemberNameAttribute

#2楼 2014-12-01
09:13
wzt  

用这个方法发布的时候要连pdb文件一起发布吧?

#3楼 2014-12-01
09:42
BinSys  

http://www.wxzzz.com/1217.html

1
protected void UpdateProperty<T>(ref T
properValue, T newValue, [CallerMemberName]
string properName
=
"")

不用PDB,编译时就把函数名给弄进去了,记得我上次研究时是编译时替换为字符串常量,不知道新版本有啥变化没

#4楼 2014-12-01
22:19
Cherbim  

string += ,差评。

抱歉!评论已关闭.