|
-
Jun 18th, 2004, 10:03 AM
#1
Thread Starter
Frenzied Member
Determine which method called a method
I have a method (Log) that logs stuff to a text file. This gets called from numerous places. Is there a way, in the Log method, to determine the name of the method that called it?
Thanks,
Mike
-
Jun 18th, 2004, 10:48 AM
#2
Frenzied Member
There's no doubt better ways, but off the top of my head, you could pass the calling method's name as an argument. You could throw an exception in the method, catch it, and parse the exception.stacktrace, although that sounds like a lot of work.
-
Jun 18th, 2004, 10:52 AM
#3
PowerPoster
Hi,
Isn't the identity of the caller in Sender?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jun 18th, 2004, 10:55 AM
#4
Frenzied Member
Isn't sender for the object that fired an event, not a method?
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jun 18th, 2004, 10:57 AM
#5
Thread Starter
Frenzied Member
Yeah, I'm lazy and did not want to send to the Log method the name of the method I'm in. One more thing to maintain if I change the method name.
Parsing the stack trace is a smart idea, it's some work, but I'd only have to do it once and never touch it again.
Taxes, what do you mean by the "identity of caller in Sender"? Can I pass an argument something like Me.CurrentMethodName?
Thanks,
Mike
-
Jun 18th, 2004, 06:28 PM
#6
Sleep mode
Check Reflection class , I think it's what you want .
-
Jun 18th, 2004, 06:37 PM
#7
Try this...
VB Code:
Imports System.Reflection
Public Class Form1
Inherits System.Windows.Forms.Form
'//Windows Form Designer generated code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
A()
End Sub
Private Sub A()
B()
End Sub
Private Sub B()
C()
End Sub
Private Sub C()
D()
End Sub
Private Sub D()
E()
End Sub
Private Sub E()
Dim st As New StackTrace
For x As Integer = 0 To st.FrameCount - 1
Diagnostics.Debug.WriteLine(st.GetFrame(x).GetMethod.Name())
Next
End Sub
End Class
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 18th, 2004, 06:51 PM
#8
PowerPoster
Originally posted by Mike Hildner
Taxes, what do you mean by the "identity of caller in Sender"? Can I pass an argument something like Me.CurrentMethodName?
Thanks,
Mike
I thought Methods, Subs & Functions were virtually the same. When a Sub or Function is called you can read the Sender.Name property. Or am I wrong?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jun 19th, 2004, 04:47 PM
#9
Thread Starter
Frenzied Member
crptcblade,
Most excellent answer. I did not even know about the StackTrace and StackFrame classes. Much appreciated. Clean, simple way to do it (I was already writing a StackTraceParser class). As a bonus, I learned something new. Nice.
Thanks crptcblade,
Mike
-
Jun 19th, 2004, 06:56 PM
#10
Frenzied Member
Well, sure, you could get an answer from people who know what they're talking about. But what would that do to my post count?
-
Jun 20th, 2004, 03:46 PM
#11
Thread Starter
Frenzied Member
LOL, salvelinus. That's why forums are good. At least you offered a good idea. Like I said, I was already writing a parser class, which, I thought would be kinda fun - regular expressions and all. Of course, not having to write it is even better. I appreciate your response to my original question. I didn't think of it.
Thanks,
Mike
-
Jun 21st, 2004, 08:59 AM
#12
Thread Starter
Frenzied Member
Pretty interesting what you find - the method names that is. I'm using this code:
VB Code:
Dim callingMethodName As String
Dim st As New StackTrace
callingMethodName = st.GetFrame(st.FrameCount - 1).GetMethod.Name
This is a service, when I call the Log method from the OnStart method, the name of the method is actually ServiceQueuedMainCallback. When called from the OnStop method, the method name is Main. Even stranger, I have a method named ReadClientCallback, which shows up as CompletionPortCallback. All the other methods show up as the name I've given them.
Don't think it's anything to worry about, but it is strange.
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|