Click here to Skip to main content
16,018,353 members
Articles / Mobile Apps
Article

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 212.9K   1   48   21
This article provides an overview for getting current and parent method name for logging purpose.

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 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:

C#
using System.Diagnostics;
using System.Reflection;

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

C#
[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(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.

License

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


Written By
Architect
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

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