Click here to Skip to main content
Licence 
First Posted 11 Aug 2004
Views 101,950
Bookmarked 39 times

Logging method name in .NET

By S Sansanwal | 11 Aug 2004
This article provides an overview for getting current and parent method name for logging purpose.
3 votes, 13.0%
1

2
7 votes, 30.4%
3
7 votes, 30.4%
4
6 votes, 26.1%
5
4.00/5 - 23 votes
3 removed
μ 3.77, σa 2.27 [?]

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


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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 3 PinmemberMarty_4620:57 23 Aug '11  
GeneralThanks PinmemberMember 47443881:28 26 Apr '11  
QuestionHow do you get the values of the method's parameters? PinmemberARopo1:56 9 Oct '09  
AnswerGot the interface name like this PinmemberARopo1:39 9 Oct '09  
QuestionHow can I get the name of the interface the method belongs to? PinmemberARopo0:32 9 Oct '09  
AnswerRe: How can I get the name of the interface the method belongs to? PinmemberS Sansanwal0:49 9 Oct '09  
GeneralI like directly use -> MethodBase.GetCurrentMethod().Name PinmemberWuJunyin6:01 4 Jun '09  
GeneralRe: I like directly use -> MethodBase.GetCurrentMethod().Name Pinmemberalessandro pungitore0:44 16 Aug '11  
GeneralGood article PinmemberDonsw18:17 12 Jan '09  
AnswerShorter way PinPopularmemberDannie Juge10:46 1 Jul '08  
GeneralRe: Shorter way Pinmemberspencepk5:59 29 Sep '10  
Generalinteresting bug Pinmembermcleodia9:25 12 Sep '07  
QuestionCompact Framework PinmemberAndreas Ringdal3:17 19 Jan '07  
GeneralIs there an equivalent in c++ Pinmembervblisp2:25 27 Jun '06  
AnswerRe: Is there an equivalent in c++ PinmemberE.Davidse4:04 4 Aug '06  
GeneralDoesn't seem to work with Web Methods Pinmemberwile_E_Coyote7:45 13 Apr '05  
GeneralRe: Doesn't seem to work with Web Methods PinmemberS Sansanwal14:29 19 May '05  
GeneralObfuscating PinmemberRayGT22:25 23 Aug '04  
GeneralPerformance considerations Pinmembercchisho4:12 19 Aug '04  
GeneralRe: Performance considerations PinmemberKevin I16:22 22 Aug '04  
GeneralRe: Performance considerations PinmemberLJMorsillo6:04 11 Mar '05  

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.

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