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
Printable View
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
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.
Hi,
Isn't the identity of the caller in Sender?
Isn't sender for the object that fired an event, not a method?
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
Check Reflection class , I think it's what you want .
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
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?Quote:
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
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
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?
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
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