Geeks With Blogs
Mike Nichols - SonOfNun Technology If I were the captain on a big steamboat...

There's probably a more elegant way to do this, but I wanted to find out which method had called the current method for logging. For example, I am calling a method in my DAO and i want to know which method from my Service layer called it...the Stack trace caught during an exception cuts me off and I could only find snippets of getting the current method name for logging purposes.

So, here's a method I drummed up to get the name of the calling-calling method:

/// <summary>

/// Helper method for getting the name of the method that got us here

/// </summary>

/// <param name="currentMethod"></param>

/// <returns></returns>

private string GetPreviousMethodName(MethodBase currentMethod)

{

string methodName = string.Empty;

try

{

StackTrace sTrace = new System.Diagnostics.StackTrace(true);

//loop through all the stack frames

for (Int32 frameCount = 0; frameCount < sTrace.FrameCount; frameCount++)

{

StackFrame sFrame = sTrace.GetFrame(frameCount);

System.Reflection.MethodBase thisMethod = sFrame.GetMethod();

//If the Type in the frame is the type that is being searched

if (thisMethod == currentMethod)

{

if (frameCount + 1 <= sTrace.FrameCount)

{

StackFrame prevFrame = sTrace.GetFrame(frameCount + 1);

System.Reflection.MethodBase prevMethod = prevFrame.GetMethod();

methodName = prevMethod.ReflectedType + "." + prevMethod.Name;

//get the method and its parameter info

//then exit out of the for loop

}

break;

}

}

}

catch (Exception)

{

//swallow all exceptions this may encounter...this is informational and mroe for convenience anyways

return string.Empty;

}

return methodName;

 

}

Posted on Thursday, June 29, 2006 10:23 PM C# | Back to top


Comments on this post: Finding calling method using reflection

# re: Finding calling method using reflection
Requesting Gravatar...
Separation of concerns.
Left by Don Pannick on Jun 30, 2006 4:56 AM

# RE: Finding calling method using reflection
Requesting Gravatar...
Exactly what I was looking for...I don't know a lot about reflection, so I wonder if there is a more
Left by Community Blogs on Aug 11, 2006 3:20 PM

# re: Finding calling method using reflection
Requesting Gravatar...
How about:

return new StackFrame(2, false).GetMethod()

Left by Haacked on Aug 11, 2006 1:00 PM

# re: Finding calling method using reflection
Requesting Gravatar...
Whoops! That should probably be

return new StackFrame(1, false).GetMethod()

In my code, I had a method called GetCallingMethod() which uses this technique, so it has to pass in 2 to be useful.
Left by Haacked on Aug 11, 2006 1:51 PM

# re: Finding calling method using reflection
Requesting Gravatar...
I found a simpler example on the internet, at:
http://www.codeproject.com/dotnet/MethodName.asp


using System.Diagnostics;
using System.Reflection;

[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 );
}
Left by FMAG on Dec 05, 2006 2:55 PM

# re: Finding calling method using reflection
Requesting Gravatar...
Nice piece of code! really a interesting area to play around. Thanks
Left by Murali on Jan 11, 2008 1:56 PM

# re: Finding calling method using reflection
Requesting Gravatar...
Here is the simple way to do it:

http://www.csharp-examples.net/reflection-calling-method-name/
Left by Ananda on Jun 30, 2008 7:18 PM

# re: Finding calling method using reflection
Requesting Gravatar...
Very useful article for get calling methods using reflection
Left by Dhiraj N. Jire on May 11, 2009 8:32 AM

# re: Finding calling method using reflection
Requesting Gravatar...
Awesome information... and horrible information :) Ok, so no worries. I just need to change the way I handle this stuff. The learning will continue.
Left by leather jackets on Dec 28, 2009 6:41 AM

# re: Finding calling method using reflection
Requesting Gravatar...
Thank you for sharing such good experience.I also like to write such things in own blog. gucci sandals very good!
Left by nikki on Jun 10, 2010 11:07 PM

# re: Finding calling method using reflection
Requesting Gravatar...
Why is it always that stack traces never give enough information?
Left by Vera Wang Mattress on Aug 20, 2010 10:54 AM

Your comment:
 (will show your gravatar)


Copyright © Mike Nichols | Powered by: GeeksWithBlogs.net