Question Monitoring methods and properties

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
Does anybody here know if this is possible somehow?

Let's say that I have something like this:

VB.NET:
Public sub WriteLog(str as string)
' Code to write to log
End Sub

Public class AwesomeClass

public sub DoSomething

End sub

public property SomeProperty as string
set

get
return "Nice"
end get
End property

End Class

I would like to call the WriteLog every time a sub, function or property is called in AwesomeClass
In this situation it would of course be easy just to add the WriteLog("Something") line for the situations here, but it's going to be a lot of work if the class has 100+ properties/methods, not to mention if I want to use this on AwesomeClass today and tomorrow I want to use it on SomeOtherClass

So basically I'm looking for an event (or something) that is raised whenever a method/property is called
But unfortunately my search has been fruitless :(
Is this even possible? I have only found something that can raise my event if an event on the class has been raised (like TextChanged on TextBox or whatever)
 
Of course you can!

You can use a Generics Dictionary(Of String, String)
In each method you would add the info:

VB.NET:
'class declaration
Dim myMethodList As New Dictionary(Of String, String)
'inside method/function
myMethodList.Add(<methodName>, <value>)
 
Is this even possible? I have only found something that can raise my event if an event on the class has been raised (like TextChanged on TextBox or whatever)

Possible or not, it's a bad idea... The performance will be horrendous. Take a look at some profiling tools? Redgate Ants Profiler might give you an output like what you say you want (and when you look at it, you'll know you don't want this feature at all)
 
Of course you can!

You can use a Generics Dictionary(Of String, String)
In each method you would add the info:

VB.NET:
'class declaration
Dim myMethodList As New Dictionary(Of String, String)
'inside method/function
myMethodList.Add(<methodName>, <value>)

That wouldn't do the trick, as I would still have to add that code for each method/property I have. That is what I'm trying to avoid


Possible or not, it's a bad idea... The performance will be horrendous

At this point performance is not an issue, and I don't want to use this for release of course. This is for debugging only

Take a look at some profiling tools? Redgate Ants Profiler might give you an output like what you say you want (and when you look at it, you'll know you don't want this feature at all)

I'll take a look at it, thanks
 
If it is for debugging only:

Either set your IDE to stop on exceptions, then look at the call stack

or look at the .StackTrace of an exception; it gives the call stack into where an app crashed.

If you want to find out the hot spots of your app (methods called often), that's profiling
 
Back
Top