0

I'm trying to get the referring method in vb.net.

e.g. I have 1 generic method (sendMail) that handle's emails, any other method can call this. I want sendMail to log an entry to the database when it sends an email. In this log i want the name of method that calls sendMail. I can do it by passing paramaters but I would like to know if sendMail can access the name of the method that calls it.

I found this article that works great in vs Is it possible to get the referring method in VB.NET? but unfortunately i'm working in a proprietary application and their IDE and the output I get from StackFrame is 'ExecuteAction at offset 1438 in file:line:column :0:0 '. I think it might be because the StackFrame used in example by Jon works in debug mode not release. (MSDN said something about debug mode but i'm not 100% sure here)

Is there another way of getting the calling method name? Or am I using StackFrame incorrectly?

Cheers in advance.

dno

Community
  • 1
  • 1
dno
  • 1

1 Answers1

0
    public string GetStackTrace()
    {
        StackTrace st = new StackTrace(true);
        StackFrame[] frames = st.GetFrames();

        return frames[1].GetMethod().Name.ToString();
    }

give it a try:
this method will most likely return the name of its caller, with a few adjustment, you cant tweak it to nest back by increasing the index of the frames array.
good luck

Tawfik Khalifeh
  • 939
  • 6
  • 21
  • pardon my ignorance. I'm a DBA. not a .NET dev. This code example is for c#? is that correct? – dno Aug 21 '12 at 04:02
  • you're completely true, the namespaces are alike in both language it wont make much of a difference, if you have any further questions am ready. i might forget to mention you should include System.Diagnostics VB: imports System.Diagnostics C#: using System.Diagnostics regards – Tawfik Khalifeh Aug 26 '12 at 13:42