---
title: "Small Tip: Getting the name of the current or calling method at runtime for debugging"
url: "https://osherove.com/blog/2006/5/14/small-tip-getting-the-name-of-the-current-or-calling-method.md"
canonical: "https://osherove.com/blog/2006/5/14/small-tip-getting-the-name-of-the-current-or-calling-method.html"
published: "2006-05-14T22:05:00"
updated: "2006-05-14T22:05:00"
tags: [".NET", ".Net 2.0"]
author: "Roy Osherove"
description: "Here's a simple little trick that you can use when using Debug Mode(though it does have its overhead, so turn it off when you don't need it). You know how you write those nasty trace messages that say \"I'm…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Small Tip: Getting the name of the current or calling method at runtime for debugging

By Roy Osherove May 14, 2006 In [.NET](https://osherove.com/blog/category/net), [.Net 2.0](https://osherove.com/blog/category/net-2-0)

**Historical article** · Originally published May 14, 2006; technical details may be outdated.

Here's a simple little trick that you can use when using Debug Mode(though it does have its overhead, so turn it off when you don't need it). You know how you write those nasty trace messages that say "I'm entering/leaving this current method"? Ain't it a nasty thing to keep writing the names by hand, or using Macros to automate it? The maintenance is just too much when refactoring method names.

You can use the class "StackTrace" located under "System.Diagnostics" to return the name of the current method (or one of it's callers up the stack. So, if you wanted to write the name of the current method with StackTrace you could write:

   **Debug.WriteLine("Entering " + new StackTrace().GetFrame(0).GetMethod().Name);**

The "0" indicated the index of the frame to show, in this case, the lowest one in the stack - the current one. "1" means the immediate caller to this method, "2" means the caller to the caller and so on. You need to watch out if you use this in Release mode, because the compiler may have some interesting optimization effects, like removing redundant method calls etc.. but it's a nice technique for tracing.

There's even a simpler way to do this by calling the MethodBase class's static "GetCurrentMethod" method:

   **Debug.WriteLine("Entering " + MethodBase.GetCurrentMethod().Name);**

But using the first method gets you the ability to look \*up\* the stack so you can write code like this:

**private void MyMethod()**

**{**

 **Debug.WriteLine("Entering " + My.Name);**

**}**

**public class My**       

**{**

   **public static string Name
        {
            get
            {
                try
                {
                    return new StackTrace().GetFrame(1).GetMethod().Name;
                }
                catch (Exception)
                {**

                    **return "Unknown Method";
                }
            }
}**

You can go further by adding other stuff to the current method like a special description attribute shown in debug mode like this:

**\[Desc("This method is just silly")\]**

**public void MyMethod()**

**{**

   **Debug.WriteLine(My.Description);**

**}**

this is possible by adding the following property to the "My" class"

    **public static string Description
        {
            get
            {
                try
                {
                    return new StackTrace().GetFrame(1).GetMethod()
                        .GetCustomAttributes(typeof(Desc),true)\[0\].Description;
                }
                catch (Exception)
                {**

                    **return string.Emtpy;
                }
            }
        }**

## More in .NET

[All posts in .NET](https://osherove.com/blog/category/net)

- May 22, 2006

    ### [Review - Automise: Automise almost anything](https://osherove.com/blog/2006/5/22/review-automise-automise-almost-anything.html)

- May 18, 2006

    ### [CLR Generics Vs. C++ Templates - some good links](https://osherove.com/blog/2006/5/18/clr-generics-vs-c-templates-some-good-links.html)

- Apr 27, 2006

    ### [Persistore - Interesting attempt at a .NET Persistence Engine? You decide.](https://osherove.com/blog/2006/4/27/persistore-interesting-attempt-at-a-net-persistence-engine-y.html)

- Apr 14, 2006

    ### [Partial Classes - Add to your Unit Testing Bag of Tricks](https://osherove.com/blog/2006/4/14/partial-classes-add-to-your-unit-testing-bag-of-tricks.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
