Click here to Skip to main content
15,115,960 members
Articles / Mobile Apps
Posted 11 Aug 2004


1 download
48 bookmarked

Logging method name in .NET

Rate me:
Please Sign up or sign in to vote.
4.00/5 (24 votes)
11 Aug 20041 min read
This article provides an overview for getting current and parent method name for logging purpose.


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;

Create a new static function named WhatsMyName and call it from the Main function.

static void Main(string[] args)
// function to display its name
private static void WhatsMyName()
    StackFrame stackFrame = new StackFrame();
    MethodBase methodBase = stackFrame.GetMethod();
    Console.WriteLine(methodBase.Name ); // Displays "WhatsmyName"
// Function to display parent function
private static void WhoCalledMe()
    StackTrace stackTrace = new StackTrace();
    StackFrame stackFrame = stackTrace.GetFrame(1);
    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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

S Sansanwal
Australia Australia
No Biography provided

Comments and Discussions

GeneralMy vote of 3 Pin
Marty_4623-Aug-11 20:57
MemberMarty_4623-Aug-11 20:57 
GeneralThanks Pin
Member 474438826-Apr-11 1:28
MemberMember 474438826-Apr-11 1:28 
QuestionHow do you get the values of the method's parameters? Pin
ARopo9-Oct-09 1:56
MemberARopo9-Oct-09 1:56 
AnswerGot the interface name like this Pin
ARopo9-Oct-09 1:39
MemberARopo9-Oct-09 1:39 
QuestionHow can I get the name of the interface the method belongs to? Pin
ARopo9-Oct-09 0:32
MemberARopo9-Oct-09 0:32 
AnswerRe: How can I get the name of the interface the method belongs to? Pin
S Sansanwal9-Oct-09 0:49
MemberS Sansanwal9-Oct-09 0:49 
GeneralI like directly use -> MethodBase.GetCurrentMethod().Name Pin
CooperWu4-Jun-09 6:01
MemberCooperWu4-Jun-09 6:01 
GeneralRe: I like directly use -> MethodBase.GetCurrentMethod().Name Pin
punggi16-Aug-11 0:44
Memberpunggi16-Aug-11 0:44 
GeneralGood article Pin
Donsw12-Jan-09 18:17
MemberDonsw12-Jan-09 18:17 
AnswerShorter way PinPopular
Dannie Juge1-Jul-08 10:46
MemberDannie Juge1-Jul-08 10:46 
GeneralRe: Shorter way Pin
spencepk29-Sep-10 5:59
Memberspencepk29-Sep-10 5:59 
Generalinteresting bug Pin
mcleodia12-Sep-07 9:25
Membermcleodia12-Sep-07 9:25 
QuestionCompact Framework Pin
Andreas Ringdal19-Jan-07 3:17
MemberAndreas Ringdal19-Jan-07 3:17 
GeneralIs there an equivalent in c++ Pin
vblisp27-Jun-06 2:25
Membervblisp27-Jun-06 2:25 
AnswerRe: Is there an equivalent in c++ Pin
E.Davidse4-Aug-06 4:04
MemberE.Davidse4-Aug-06 4:04 
GeneralDoesn't seem to work with Web Methods Pin
wile_E_Coyote13-Apr-05 7:45
Memberwile_E_Coyote13-Apr-05 7:45 
GeneralRe: Doesn't seem to work with Web Methods Pin
S Sansanwal19-May-05 14:29
MemberS Sansanwal19-May-05 14:29 
GeneralObfuscating Pin
RayGT23-Aug-04 22:25
MemberRayGT23-Aug-04 22:25 
GeneralPerformance considerations Pin
cchisho19-Aug-04 4:12
Membercchisho19-Aug-04 4:12 
GeneralRe: Performance considerations Pin
Kevin I22-Aug-04 16:22
MemberKevin I22-Aug-04 16:22 
GeneralRe: Performance considerations Pin
LJMorsillo11-Mar-05 6:04
MemberLJMorsillo11-Mar-05 6:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.