Determening where a function was called from

  • Thread starter Christopher Kimbell
  • Start date
C

Christopher Kimbell

The Trace class in the .NET framework provides basic tracing capabilities. I
would like to create my own tracer that includes the date and time the trace
occured and what function and class the trace was called from.

The date and time is no problem, the tricky bit is to determine where the
function was called from, I only want to go one level up, not the whole call
stack. Is there any way to use reflection to determine the calling function?
I had a look at the MethodBase class but I can't find any promising
properties or methods. Since the call stack is used when getting the stack
trace from an exception and for security checks, I was hoping it would be
possible to get hold of something I could use.

Chris
 
S

Sunny

Hi Chris,


The Trace class in the .NET framework provides basic tracing capabilities. I
would like to create my own tracer that includes the date and time the trace
occured and what function and class the trace was called from.

The date and time is no problem, the tricky bit is to determine where the
function was called from, I only want to go one level up, not the whole call
stack. Is there any way to use reflection to determine the calling function?
I had a look at the MethodBase class but I can't find any promising
properties or methods. Since the call stack is used when getting the stack
trace from an exception and for security checks, I was hoping it would be
possible to get hold of something I could use.

Chris

I'm using this:

private static string GetCallerInfo()
{
StackTrace st = new StackTrace(3);
return st.GetFrame(0).GetMethod().DeclaringType.FullName +
"." + st.GetFrame(0).GetMethod().Name;
}

Sunny
 
C

Christopher Kimbell

Thanks a bunch.

Chris

Sunny said:
Hi Chris,




I'm using this:

private static string GetCallerInfo()
{
StackTrace st = new StackTrace(3);
return st.GetFrame(0).GetMethod().DeclaringType.FullName +
"." + st.GetFrame(0).GetMethod().Name;
}

Sunny
 

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