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

Logging method name in .NET

By , 11 Aug 2004
 

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:

using System.Diagnostics;
using System.Reflection;

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

[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

About the Author

S Sansanwal
Architect
Australia Australia
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 3memberMarty_4623 Aug '11 - 19:57 
GeneralThanksmemberMember 474438826 Apr '11 - 0:28 
QuestionHow do you get the values of the method's parameters?memberARopo9 Oct '09 - 0:56 
AnswerGot the interface name like thismemberARopo9 Oct '09 - 0:39 
QuestionHow can I get the name of the interface the method belongs to?memberARopo8 Oct '09 - 23:32 
AnswerRe: How can I get the name of the interface the method belongs to?memberS Sansanwal8 Oct '09 - 23:49 
GeneralI like directly use -> MethodBase.GetCurrentMethod().NamememberWuJunyin4 Jun '09 - 5:01 
GeneralRe: I like directly use -> MethodBase.GetCurrentMethod().Namememberalessandro pungitore15 Aug '11 - 23:44 
GeneralGood articlememberDonsw12 Jan '09 - 17:17 
AnswerShorter waymemberDannie Juge1 Jul '08 - 9:46 
GeneralRe: Shorter waymemberspencepk29 Sep '10 - 4:59 
Generalinteresting bugmembermcleodia12 Sep '07 - 8:25 
QuestionCompact FrameworkmemberAndreas Ringdal19 Jan '07 - 2:17 
GeneralIs there an equivalent in c++membervblisp27 Jun '06 - 1:25 
AnswerRe: Is there an equivalent in c++memberE.Davidse4 Aug '06 - 3:04 
GeneralDoesn't seem to work with Web Methodsmemberwile_E_Coyote13 Apr '05 - 6:45 
GeneralRe: Doesn't seem to work with Web MethodsmemberS Sansanwal19 May '05 - 13:29 
GeneralObfuscatingmemberRayGT23 Aug '04 - 21:25 
GeneralPerformance considerationsmembercchisho19 Aug '04 - 3:12 
GeneralRe: Performance considerationsmemberKevin I22 Aug '04 - 15:22 
GeneralRe: Performance considerationsmemberLJMorsillo11 Mar '05 - 5:04 

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

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