转:.NET获取当前方法名或调用此方法的方法名
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 ofStackFrame
.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获取当前方法名或调用此方法的方法名的更多相关文章
- java根据方法名动态调用invoke方法!
public class Activity { public void deal(String name, long id) { System.out.println(name + id + &quo ...
- Android获取APK包名的几种方法
Android获取APK包名的几种方法:1.adb shell pm list package -f | findstr 关键字 #只能获取到包名,主Activity名无法获取到 2.使用aapt-- ...
- PHP获取文件后缀名的三种方法
如下: <? PHP获取文件后缀名的几种方法1: function get_file_type($filename){ $type = substr($filename, strrpos($fi ...
- 使用js方法时,调用的方法名明明一致,但就是不管用,解决
前提:代码全部写对 问题:调用的方法名明明一致,但就是不管用 举例:写了个function delete(){}方法, 点击调用delete方法,onclik="delete()" ...
- PHP中获取文件扩展名的N种方法
PHP中获取文件扩展名的N种方法 从网上收罗的,基本上就以下这几种方式: 第1种方法:function get_extension($file){substr(strrchr($file, '.'), ...
- iOS JS 交互之利用系统JSContext实现 JS调用OC方法以及Objective-C调用JavaScript方法
ios js 交互分为两块: 1.oc调用js 这一块实现起来比较简单, 我的项目中加载的是本地的html,js,css,需要注意的是当你向工程中拖入这些文件时,选择拷贝到工程中,(拖入的文件夹是蓝色 ...
- C# 解析js方法,并调用js方法
本文转载:http://www.cnblogs.com/StudyLife/archive/2013/03/11/2953516.html 本文不是基于B/S的 后台调用前台js方法,而是给你一段js ...
- 调用start()方法和直接调用run()方法的区别
调用start()方法和直接调用run()方法的区别 新建一个线程,只需要使用new关键字创建一个线程对象,并且调用start()方法即可. Thread thread = new Thread(); ...
- 134-PHP子类重写父类方法,并调用父类方法
<?php class father{ //定义father类 public function method(){ //定义方法 echo '<br />father method' ...
随机推荐
- 小白科普之JavaScript的数组
一.与其他语言数据的比较 相同点:有序列表 不同点:js的数组的每一项可以保存任何类型的数据:数组的大小是可以动态调整的 二.数组创建的两种方法 1) var colors = new ...
- 找不到提交和更新按钮,subversion不见了,无法更新和上传代码
1.查看settings/plugins/下有没有subversion 插件,如果有,确保勾上. 2.VCS->Enable Version Control Integration...
- PHP 遍历数组的方法汇总
1. foreach() foreach()是一个用来遍历数组中数据的最简单有效的方法. #example1: <?php $colors= array('red','blue','green' ...
- Windows上搭个Nginx集群环境玩玩
一.在windows上安装nginx 1.从这里下载nginx的windows版本 2.把压缩文件解压至c盘根目录,并将文件夹重命名成nginx 3.在conf目录下的nginx.conf文件中,指定 ...
- RAID阵列的初始化与管理
如果我们创建RAID阵列的目的是新部署一台服务器,我们建议所有新创建的RAID阵列都应该做初始化操作,这样,硬盘上原有的用户数据将被清除,以便进行后续的系统,软件安装. 转自: http://zh.c ...
- 【SpringMVC】SpringMVC系列5之@RequestHeader 映射请求头属性值
5.@RequestHeader 映射请求头属性值 5.1.概述 请求头包含了若干个属性,服务器可据此获知客户端的信息,通过 @RequestHeader 即可将请求头中的属性值绑定到处理方法的入参中 ...
- Python多线程(3)——Queue模块
Queue模块支持先进先出(FIFO)队列,支持多线程的访问,包括一个主要的类型(Queue)和两个异常类(exception classes). Python 2 中的Queue模块在Python ...
- Java for LeetCode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- protostuff简单应用
protobuf是谷歌推出的与语言无关.平台无关的通信协议,一个对象经过protobuf序列化后将变成二进制格式的数据,所以他可读性差,但换来的是占用空间小,速度快.居网友测试,它的序列化效率是xml ...
- mysql_1
1.mysql> select NOW();等效于select user()\g+---------------------+| NOW() |+---------- ...