tracing trouble

  • Thread starter Thread starter Corno
  • Start date Start date
C

Corno

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?

Corno
 
Corno said:
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
 
Public Function GetMethodName() As String
Dim st As New StackTrace(False)
Return st.GetFrame(st.FrameCount - 2).GetMethod.Name()
End Function

No idea if its any more or less efficient or whatever, but here's
something akin to what I use:

Imports System.Reflection.MethodBase
.. . .
Public Sub foo()
BeginMethod(GetCurrentMethod().Name)

'do whatever

EndMethod(GetCurrentMethod().Name)
End Sub

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top