How do I get info regarding the calling method

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
 
C

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
 
K

Kulwant

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..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top