Click here to Skip to main content
Click here to Skip to main content

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

Comments and Discussions

GeneralMy vote of 3 PinmemberMarty_4623-Aug-11 19:57 
GeneralThanks PinmemberMember 474438826-Apr-11 0:28 
QuestionHow do you get the values of the method's parameters? PinmemberARopo9-Oct-09 0:56 
AnswerGot the interface name like this PinmemberARopo9-Oct-09 0:39 
QuestionHow can I get the name of the interface the method belongs to? PinmemberARopo8-Oct-09 23:32 
AnswerRe: How can I get the name of the interface the method belongs to? PinmemberS Sansanwal8-Oct-09 23:49 
GeneralI like directly use -> MethodBase.GetCurrentMethod().Name PinmemberWuJunyin4-Jun-09 5:01 
GeneralRe: I like directly use -> MethodBase.GetCurrentMethod().Name Pinmemberalessandro pungitore15-Aug-11 23:44 
GeneralGood article PinmemberDonsw12-Jan-09 17:17 
AnswerShorter way PinmemberDannie Juge1-Jul-08 9:46 
GeneralRe: Shorter way Pinmemberspencepk29-Sep-10 4:59 
Generalinteresting bug Pinmembermcleodia12-Sep-07 8:25 
QuestionCompact Framework PinmemberAndreas Ringdal19-Jan-07 2:17 
GeneralIs there an equivalent in c++ Pinmembervblisp27-Jun-06 1:25 
AnswerRe: Is there an equivalent in c++ PinmemberE.Davidse4-Aug-06 3:04 
GeneralDoesn't seem to work with Web Methods Pinmemberwile_E_Coyote13-Apr-05 6:45 
GeneralRe: Doesn't seem to work with Web Methods PinmemberS Sansanwal19-May-05 13:29 
GeneralObfuscating PinmemberRayGT23-Aug-04 21:25 
GeneralPerformance considerations Pinmembercchisho19-Aug-04 3:12 
GeneralRe: Performance considerations PinmemberKevin I22-Aug-04 15:22 
GeneralRe: Performance considerations PinmemberLJMorsillo11-Mar-05 5:04 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 12 Aug 2004
Article Copyright 2004 by S Sansanwal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid