Geeks With Blogs
Patrice Calve Life's short, have fun

A long time ago, I wrote a very simple log method.  It was used to print the number of times a function/method was called.  I just changed it a bit and added an optional dumpAll parameter for printing the whole results.

It tracks 3 things: the name of the method, the number of times it was called, and stack level.

    Private Sub log(Optional ByVal dumpAll As Boolean = False)
        Static Dim h As New Generic.Dictionary(Of String, Integer)()
        If dumpAll Then
            For Each d As Generic.KeyValuePair(Of String, Integer) In h
                System.Diagnostics.Debug.Print("[{0}] was called [{1}] times", d.Key, d.Value)
            Dim st As New StackTrace(True)
            Dim m As System.Reflection.MethodBase = st.GetFrame(1).GetMethod()
            If Not h.ContainsKey(m.Name) Then
                h.Add(m.Name, 1)
                h(m.Name) = h(m.Name) + 1
            End If
            System.Diagnostics.Debug.Print("Method [{0}],(stack [{1}]),called [{2}] times", m.Name, st.FrameCount, h(m.Name))
        End If
    End Sub


All you need to do is to add the call to the log() method in your method/function.


   Public Overrides Sub MyMethod()
    End Sub

It would be nice to have a macro/addin to add/remove this log() call to every method/function in a class.  Mmm...


Posted on Wednesday, May 6, 2009 9:47 AM | Back to top

Comments on this post: Very simple Log() method in VB

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Patrice Calvé | Powered by: