6

Basically i've got a web service that i'm trying to put some kind of usage logging in. To do this i've created a class with a logging method. I instatiate the class on the service and then call the logging method in each of the web methods.

I'm trying to find a way of getting the name of the method that has called the loggong method

Public sub tstmethod
log_object.log_method
end sub

in this case the returned name i'm looking for is "tstmethod"

Everywhere i've seen has said to either use

Dim stackframe As New Diagnostics.StackFrame
Return stackframe.GetMethod.Name.ToString

Or to use

Dim stackframe As New Diagnostics.StackFrame
Return stackframe.GetMethod.DeclaringType.FullName

which i call inside the logging method

getmethod.name.tostring returns the name of the logging method

getmethod.declaringtype.fullname returns the name of the logging class

no matter what i do i cannot find a way of getting the name of the method that called the logging method in this case "tstmethod"

8 Answers 8

13

You need to instantiate the StackFrame with an appropriate constructor. For example,

Dim stackframe As New Diagnostics.StackFrame(1)

will skip one stack frame (the logging method itself) and get the frame for the caller of that method.

That said - if at all possible, I'd strongly recommend using a standard logging mechanism such as log4net.

Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't work in production
12

I think the above statement can be made into one single line:

MsgBox((New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name)

This will display a Message Box as with calling method name.

Comments

4

VB.Net

System.Reflection.MethodBase.GetCurrentMethod.Name()

1 Comment

Won't this just get the name of the current method? I think the OP is asking how to get the name of the method that called the current method.
1

You could do this:

Imports System.Runtime.CompilerServices

    Private Sub LogError(message As String, <CallerMemberName> Optional caller As String = null)
'Logging logic here using caller
    End Sub

Comments

0

You should be able to get this from the StackFrame with the constructor telling it how many frames to go up the chain, so getting the parent would use 1 (the frame is zero based, so 0 is the current method):

Dim stackTrace as New Diagnostics.StackFrame(1)
Return stackTrace.GetMethod.Name

Comments

0

Do not try using StackFrame. It will work only in debug mode which is a killer for application speed.

There is no way you can get this info in Release mode, you should consider changing design and adding parameter.

6 Comments

There's no need to downvote me - I just pointed out how to make StackFrame work for the questioner, but I specifically recommended using log4net (which works in Release mode as well as Debug mode). You should only downvote unhelpful answers - by my reckoning, my answer was not unhelpful.
BTW I wasn't the one to downvote you. I'd never downvote without leaving a comment giving the reason for the downvote.
BTW, reason was my post. I downvoted all StackFrame posts because this was one month of work to find out why app doesn't work in release mode.
-1: It does work in release mode (if you modify the project properties to include the pdb)
I couldn't find this particular setting in project properties. And no informations in MSDN about it. Maybe you mix-up knowledge about C++?
|
0

I add the calling form as the first parameter for all sub routines and functions like calledfunction(me,para1,para2......) so that i can identify the calling form and use its name or text in the called routine

Comments

-1

I am using using this code for getting the calling method

string methodName = new StackFrame( 1, true ).GetMethod().Name;
methodName = " [" + methodName + "]";
return methodName;

remember to add namespace Diagnostics

1 Comment

Isn't setting fNeedFileInfo to true (second argument to StackFrame constructor) a bit unnecessary in this case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.