.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Programmatically determine calling method at execution time?
 

Programmatically determine calling method at execution time?

Without using StackFrame, how might one go about determining the method that called the current method? I need this for logging but do not want to use StackFrame since it has difficulties outside of IDE environment. Looked and looked - no can find.

For example, method A and B each call method C. From method C, I want to be able to log who the parent method was that called me.

Thanks.

Matty4242
From the documentation of StackFrame.GetFileName:

"
Gets the file name that contains the code that is executing. This information is typically extracted from the debugging symbols for the executable".

Return Value

The file name. -or- a null reference (Nothing in Visual Basic) if the file name cannot be determined

This of course means that you'll get a null (Nothing) if there is no debugging information available. Which would be the case when you run the Release version of your program. Or when the stack frame references a .NET method. I showed you an alternative that doesn't rely on the debugging information (StackTrace.ToString), you might want to consider using it...

nobugz

Correct, in order to get the FileName property, you require info in the PDB files as well as debug information injected into the assembly code. Without this data, it's impossible to determine what the filename is. That has nothing to do with the IDE really, but rather with the assemblies themselves.

Nobugz's code sample is the best way to go. Is there some reason you can't use that?

Rob Teixeira
What problems are you having ouside the IDE? As far as I'm aware, there is no record of the method call chain outside the stack (where it would logically be anyway). The only other way to handle this is for you to create your call chain recording structure and populate it with each call (per thread). However, that's a lot of effort to code into every method call.
Rob Teixeira
This code gave me a complete stack trace, outside the IDE both in debug and release builds:

For ix As Integer = 0 To 9999
Dim frm As StackFrame = New StackFrame(ix, True)
If frm.GetMethod Is Nothing Then Exit For
Dim stt As New StackTrace(frm)
ListBox1.Items.Add(stt.ToString)
Next

Inside method C, you'd get the method B reference with New StackFrame(1, True) and the method A reference with New StackFrame(2, True)

nobugz

Thanks for your feedback.  It has helped me to identify the statement that is failing at run time outside the IDE.  After the statement:

Dim frm As StackFrame = New StackFrame(1, True)

I try to reference frm.GetFileName.ToString and this aborts with the following:

UOSample1 Unhandled Exception :

   at UOSample1.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
   at UOSample1.My.MyApplication.OnCreateMainForm() in C:\Data\CompanyNameVB.NET\Sample Projects\UOSample1\UOSample1\My Project\Application.Designer.vb:line 35
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

[UOSample1]UOSample1.My.MyProject+MyForms.Create__Instance__()

 

Why would this method work in the IDE but not outside the IDE?  Or am I missing something else here?

Thanks.

 

 

 

Matty4242
frm.GetFileName will return Nothing for methods that are contained in the .NET framework assemblies. This also happens in the IDE when I try it.
nobugz

Sorry, I don't quite understand what you are saying on this. Are you saying you get this same error when you run this frm.GetFileName in the IDE? If so, why am I NOT getting this error in the IDE? Also, any references that you can cite in help that explains when GetFileName works and when it doesn't?

Thanks again for your help.

Matty4242
From the documentation of StackFrame.GetFileName:

"
Gets the file name that contains the code that is executing. This information is typically extracted from the debugging symbols for the executable".

Return Value

The file name. -or- a null reference (Nothing in Visual Basic) if the file name cannot be determined

This of course means that you'll get a null (Nothing) if there is no debugging information available. Which would be the case when you run the Release version of your program. Or when the stack frame references a .NET method. I showed you an alternative that doesn't rely on the debugging information (StackTrace.ToString), you might want to consider using it...

nobugz

Correct, in order to get the FileName property, you require info in the PDB files as well as debug information injected into the assembly code. Without this data, it's impossible to determine what the filename is. That has nothing to do with the IDE really, but rather with the assemblies themselves.

Nobugz's code sample is the best way to go. Is there some reason you can't use that?

Rob Teixeira

Thanks guys - nothing like one more 2 by 4 across the head to get me thinking straight. Yes, indeed Nobugz's code does work, inside & outside the IDE. I guess I will have to parse the StackTrace.ToString to get just the file name and file line number, unless you know a better way.

Thanks again for not giving up on me!

Matty4242

You can use google to search for other answers

Custom Search

More Threads

• Questions about Application Settings
• Generating a Self Signed X509 Certificate
• Windows Services and COM interop
• Writing numbers as strings in a .csv file
• Issues with the Timer Class!
• Base Class to Sub Class
• In C#, what is the difference between System.String and System.string (lowercase)
• FileStream.Close doesn't seem to free the file.
• UnauthorizedAccessException?
• Not able to set stacksize in .NET 2.0 threads in Windows 2000 SP4