While logging or tracing an application I frequently need to know
where the code is executing - the class.method name.
One way to obtain this information is:
Me.GetType.Name & "." &
System.Reflection.MethodInfo.GetCurrentMethod.Name
The problem with this is that if you need to use it numerous times in
a method it makes the code look awfully messy.
Another approach would be to define a method level variable and set it
at the start of the method - the trouble with this is that it executes
every time the method is called. You could add an if statement to
test the tracing variable but again this makes the code look messy.
I am curious as to how others may have solved this problem.
Ideally what I would like is a tracing call like:
Trace.WritelineIf(Switch.TraceVerbose, SomeClass.FormatMessage("My
Tracing Message")
where the FormatMessage routine could determine the method signature
of the caller and format a string to include the date, time, thread,
and class.method followed by the Message.
Using the StackFrame it is possible to get at the callers method name
but not it's class name.
Any ideas?
I've looked at the Microsoft Application Logging Block but for some
reason this information (Class.Method) is not part of what they log.
|