Click here to Skip to main content
13,448,729 members (54,384 online)
Click here to Skip to main content
Add your own
alternative version


48 bookmarked
Posted 11 Aug 2004

Logging method name in .NET

, 11 Aug 2004
Rate this:
Please Sign up or sign in to vote.
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

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
Marty_4623-Aug-11 19:57
memberMarty_4623-Aug-11 19:57 
GeneralThanks Pin
Member 474438826-Apr-11 0:28
memberMember 474438826-Apr-11 0:28 
QuestionHow do you get the values of the method's parameters? Pin
ARopo9-Oct-09 0:56
memberARopo9-Oct-09 0:56 
AnswerGot the interface name like this Pin
ARopo9-Oct-09 0:39
memberARopo9-Oct-09 0:39 
QuestionHow can I get the name of the interface the method belongs to? Pin
ARopo8-Oct-09 23:32
memberARopo8-Oct-09 23:32 
AnswerRe: How can I get the name of the interface the method belongs to? Pin
S Sansanwal8-Oct-09 23:49
memberS Sansanwal8-Oct-09 23:49 
GeneralI like directly use -> MethodBase.GetCurrentMethod().Name Pin
WuJunyin4-Jun-09 5:01
memberWuJunyin4-Jun-09 5:01 
GeneralRe: I like directly use -> MethodBase.GetCurrentMethod().Name Pin
alessandro pungitore15-Aug-11 23:44
memberalessandro pungitore15-Aug-11 23:44 
GeneralGood article Pin
Donsw12-Jan-09 17:17
memberDonsw12-Jan-09 17:17 
AnswerShorter way Pin
Dannie Juge1-Jul-08 9:46
memberDannie Juge1-Jul-08 9:46 
GeneralRe: Shorter way Pin
spencepk29-Sep-10 4:59
memberspencepk29-Sep-10 4:59 
Generalinteresting bug Pin
mcleodia12-Sep-07 8:25
membermcleodia12-Sep-07 8:25 
QuestionCompact Framework Pin
Andreas Ringdal19-Jan-07 2:17
memberAndreas Ringdal19-Jan-07 2:17 
GeneralIs there an equivalent in c++ Pin
vblisp27-Jun-06 1:25
membervblisp27-Jun-06 1:25 
AnswerRe: Is there an equivalent in c++ Pin
E.Davidse4-Aug-06 3:04
memberE.Davidse4-Aug-06 3:04 
GeneralDoesn't seem to work with Web Methods Pin
wile_E_Coyote13-Apr-05 6:45
memberwile_E_Coyote13-Apr-05 6:45 
GeneralRe: Doesn't seem to work with Web Methods Pin
S Sansanwal19-May-05 13:29
memberS Sansanwal19-May-05 13:29 
GeneralObfuscating Pin
RayGT23-Aug-04 21:25
memberRayGT23-Aug-04 21:25 
GeneralPerformance considerations Pin
cchisho19-Aug-04 3:12
membercchisho19-Aug-04 3:12 
GeneralRe: Performance considerations Pin
Kevin I22-Aug-04 15:22
memberKevin I22-Aug-04 15:22 
GeneralRe: Performance considerations Pin
LJMorsillo11-Mar-05 5:04
memberLJMorsillo11-Mar-05 5: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180318.3 | Last Updated 12 Aug 2004
Article Copyright 2004 by S Sansanwal
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid