"Corno" <Corno@%SPAM%dds.nl> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
Hi all,
My collegue finds tracing very important and therefore we have agreed that
we will do quite a lot of it.
The first and last line of every function will contain a trace stating
which
function has started or ended (stack tracing), and in between we can write
relevant information;
Public Sub AddOne(ByRef counter As Integer)
TraceStartFunction("AddOne")
'Do it's thing
counter = counter + 1
Trace("work is done")
TraceEndFunction("AddOne")
End Sub
It seems to me that it should be possible to do this more efficiently.
Is there a way to combine automatic stack tracing with manual trace
messages?
I think this is about the best you can do. It gets the method name from the
stack trace and stores it in a local static variable so you only have to
generate a stack trace the first time it's run.
Your methods end up looking like this
Public Sub foo()
Static methodName As String = GetMethodName()
BeginMethod(methodName)
'do whatever
EndMethod(methodName)
End Sub
David
Imports System.Diagnostics
Public Function GetMethodName() As String
Dim st As New StackTrace(False)
Return st.GetFrame(st.FrameCount - 2).GetMethod.Name()
End Function
Public Sub BeginMethod(ByVal methodName As String)
Trace.WriteLine(String.Format("Begin {0}", methodName))
End Sub
Public Sub EndMethod(ByVal methodName As String)
Trace.WriteLine(String.Format("End {0}", methodName))
End Sub
Public Sub foo()
Static methodName As String = GetMethodName()
BeginMethod(methodName)
'do whatever
EndMethod(methodName)
End Sub
Sub Main()
Trace.Listeners.Add(New TextWriterTraceListener(Console.Out))
foo()
foo()
foo()
End Sub