Introduction

Before .NET, we were always looking for a way to log current method name in a log file for better logging. But, there were no functionalities that could have helped in this, and it was left as an uncompleted job.

But, with .NET, we could easily find out the name of the current method or parent method. This has been accomplished by StackFrame, StackTrace, and MethodBase classes in System.Diagnostics and System.Reflection namespaces.

  • StackFrame provides information about function call on stack call for current process.
  • StackTrace is a collection of StackFrame.
  • MethodBase is an abstract class containing information about current method.

Note: When an exception occurs in the method, exception object contains a reference to the StackTrace object that could be used to log the method name. But for logging a method name without an error generated, we need to read it from the stack, using StackFrame class.

In the sample, MethodBase object would reference to current function on stack call returned by StackFrame object. To get the parent method, we would use StackTrace to get parent’s StackFrame object.

Create a new console application:

Add namespaces:

using System.Diagnostics;
using System.Reflection; [STAThread]
static void Main(string[] args)
{
WhatsMyName();
}
// function to display its name
private static void WhatsMyName()
{
StackFrame stackFrame = new StackFrame();
MethodBase methodBase = stackFrame.GetMethod();
Console.WriteLine(methodBase.Name ); // Displays “WhatsmyName”
WhoCalledMe();
}
// Function to display parent function
private static void WhoCalledMe()
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame();
MethodBase methodBase = stackFrame.GetMethod();
// Displays “WhatsmyName”
Console.WriteLine( " Parent Method Name {0} ", methodBase.Name );
}

Note: This feature is not available in .NET Compact Framework as StackFrame class is unavailable. For that, you would need to use same old method of manually passing method name to the logging function.

http://www.codeproject.com/Articles/7964/Logging-method-name-in-NET

http://www.cnblogs.com/ndaysy/p/3297768.html

常用方式:

StackFrame sf = new StackFrame();
log.Error(sf.GetMethod().DeclareTypeName);

转:.NET获取当前方法名或调用此方法的方法名的更多相关文章

  1. java根据方法名动态调用invoke方法!

    public class Activity { public void deal(String name, long id) { System.out.println(name + id + &quo ...

  2. Android获取APK包名的几种方法

    Android获取APK包名的几种方法:1.adb shell pm list package -f | findstr 关键字 #只能获取到包名,主Activity名无法获取到 2.使用aapt-- ...

  3. PHP获取文件后缀名的三种方法

    如下: <? PHP获取文件后缀名的几种方法1: function get_file_type($filename){ $type = substr($filename, strrpos($fi ...

  4. 使用js方法时,调用的方法名明明一致,但就是不管用,解决

    前提:代码全部写对 问题:调用的方法名明明一致,但就是不管用 举例:写了个function delete(){}方法, 点击调用delete方法,onclik="delete()" ...

  5. PHP中获取文件扩展名的N种方法

    PHP中获取文件扩展名的N种方法 从网上收罗的,基本上就以下这几种方式: 第1种方法:function get_extension($file){substr(strrchr($file, '.'), ...

  6. iOS JS 交互之利用系统JSContext实现 JS调用OC方法以及Objective-C调用JavaScript方法

    ios js 交互分为两块: 1.oc调用js 这一块实现起来比较简单, 我的项目中加载的是本地的html,js,css,需要注意的是当你向工程中拖入这些文件时,选择拷贝到工程中,(拖入的文件夹是蓝色 ...

  7. C# 解析js方法,并调用js方法

    本文转载:http://www.cnblogs.com/StudyLife/archive/2013/03/11/2953516.html 本文不是基于B/S的 后台调用前台js方法,而是给你一段js ...

  8. 调用start()方法和直接调用run()方法的区别

    调用start()方法和直接调用run()方法的区别 新建一个线程,只需要使用new关键字创建一个线程对象,并且调用start()方法即可. Thread thread = new Thread(); ...

  9. 134-PHP子类重写父类方法,并调用父类方法

    <?php class father{ //定义father类 public function method(){ //定义方法 echo '<br />father method' ...

随机推荐

  1. 【视频】从零开始编写第一个PHP扩展

    Rango会讲解在Linux下从零开始写一个PHP扩展,并编译安装到PHP里,一直到执行扩展中的函数.包含的内容有: 为什么要开发PHP扩展 ext_skel工具的使用 修改config.m4 php ...

  2. 2012年湖南省程序设计竞赛E题 最短的名字

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1115 解题报告:输入n个字符串,让你求出可以用来区别这些字符串的最少的前缀总共有多少个字 ...

  3. shell笔记-local、export用法 、declare、set

    local一般用于局部变量声明,多在在函数内部使用.     1.    Shell脚本中定义的变量是global的,其作用域从被定义的地方开始,到shell结束或被显示删除的地方为止.     2. ...

  4. qcow2文件压缩

    qemu-img convert -O qcow2 /path/old.img.qcow2 /path/new.img.qcow2 转自:https://havee.me/linux/2011-09/ ...

  5. 对于sharepoint 的解决方案的实际说明

    对于sharepoint 的解决方案  实际上就是cab的包 你把***.wsp改为***.cab我们就可以查看这个包中的所有内容了

  6. codeigniter load_class

    2014年7月7日 14:33:36 函数定义在 system/core/common.php中 使用方法是 load_class('Hooks', 'core'): 在core目录内寻找Hooks类 ...

  7. mysql varchar

    2013年9月13日 14:58:43 MYSQL的varchar数据类型 先说结论: 如果设置字段长度为30,字符编码为utf8,那么一个汉字算一个字符,一个数字算一个字符,一个字母也算一个字符 版 ...

  8. -fomit-frame-pointer 编译选项在gcc 4.8.2版本中的汇编代码研究

    #include void fun(void) { printf("fun"); } int main(int argc, char *argv[]){ fun(); return ...

  9. Java for LeetCode 030 Substring with Concatenation of All Words【HARD】

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  10. vs 附加包含目录属性

    如果是在属性页里头添加了路径,则当程序拷贝到其他电脑上头的话,这个包含目录仍然存在,这就是与添加环境变量的区别.如果是通过添加环境变量配置的路径,则换了台电脑,这个路径就没有了,需要重新配置.