How do I get info regarding the calling method

  • Thread starter Thread starter CGuy
  • Start date Start date
C

CGuy

Hi,

I'm trying to implement a custom trace listener. I would like the
WriteLine method of this tracelistener to also write out information
regarding the method from which Trace was called. For example,

public void Method1()
{
Trace.WriteLine("Sample Text");
}

public void Method2()
{
Trace.WriteLine("Sample Text");
}

Executing the aboce 2 Trace.WriteLine methods should also give me the name
of the method and its signature in the trace listener

Regards,
CGuy
 
Thank you guys..

Here is how I finally did it

sTrace = new StackTrace(true);
//loop through all the stack frames
for(Int32 frameCount = 0; frameCount < sTrace.FrameCount; frameCount++)
{
sFrame = sTrace.GetFrame(frameCount);
MethodBase currentMethod = sFrame.GetMethod();
//If the Type in the frame is the type that is being searched
if(currentMethod.ReflectedType.FullName == current.FullName)
{
//get the method and its parameter info
//then exit out of the for loop
break;
}
}

Thanks again
CGuy
 
I would like to use this bit of code but am having trouble
working out how the variable current is deriver. Can you please advise..
 
Back
Top