转:.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.
StackFrameprovides information about function call on stack call for current process.StackTraceis a collection ofStackFrame.MethodBaseis 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' ...
随机推荐
- 压缩js和css, IIS开启Etags, IIS开启Gzip
我们在前端页面性能调优时,经常会压缩js和css,下面列出几个比较好用的在线工具. http://www.jb51.net/tools/jsmin/index.htm http://javascrip ...
- Entity Framework在SaveChanges()之前拿到对象的自增长ID
现有两张表A.B在一个dbContext中更新,且A表的ID为B的外键, Insert(A); B.aid=A.ID; Insert(B); SaveChanges() 按照这个顺序就可以了满足要求, ...
- 手动安装ubuntu视频播放器插件的方法
新安装的ubuntu14.04在浏览器里面都不能看视频,提示缺少播放器插件,而且有一个安装的按钮,但是点击之后往往提示找不到,这就要手动安装了.第一步:首先运行一下更新命令吧sudo apt-get ...
- UISerachBar / UISearchDisplayController
1. UISerachBar 继承与UIView, 包含uitextfield, 并且实现了uitextfielddelegate代理的主要内容 含有取消按钮, 默认不显示 2. UISerachDi ...
- reverse array java
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import ...
- different between unicorn / unicorn_rails
$ unicorn_rails -h Usage: unicorn_rails [ruby options] [unicorn_rails options] [rackup config file] ...
- CentOS 关闭蜂鸣器声音
也许你会遇到像我这样的情况,每次使用Linux终端,当听到发出“嘀嘀”的声音时候,我都有种把我的机箱拆掉把那个内置的蜂鸣装置拽下来的冲动.按 Tab时候“嘀嘀”,按空格时候“嘀嘀”,每个在vi中错误的 ...
- 简单方便地扩充Python的系统路径
参考: http://www.elias.cn/Python/PythonPath?from=Develop.PythonPath http://v2in.com/pth-file-usage-in- ...
- java Long的iniValue出错
Long l1 = 2500000000L; l1.intValue() 的值是负数 这里 System.out.println(Integer.MAX_VALUE); // 2147483647最大 ...
- JavaScript关闭窗口的同时打开新页面的方法
做网页的时候需要弹出一个小窗口,然后要实现一个功能就是鼠标点击超链接关闭小窗口并打开一个新页面,就如同下图: 这是一个小窗口,点击超链接这个窗口会关闭并且会正常在浏览器打开新页面,首先写js关闭窗口的 ...