determining module/class and function/subroutine currently being run

  • Thread starter Thread starter Brad Pears
  • Start date Start date
B

Brad Pears

I am developing a simple error message class and would like the be able to
generate as part of the message the curent class/module/form and function or
sub that the error was generated in without having to hardcode this info to
be passed to the error message class. I can get the stack trace using
system.environment.stacktrace - but this is too much info. I just want to
display the function/sub name where the error was generated and the parent
name of that sub/function

Any ideas?

Thanks, Brad

Thanks, Brad
 
I am developing a simple error message class and would like the be
able to generate as part of the message the curent class/module/form
and function or sub that the error was generated in without having to
hardcode this info to be passed to the error message class. I can get
the stack trace using system.environment.stacktrace - but this is too
much info. I just want to display the function/sub name where the
error was generated and the parent name of that sub/function


System.Reflection.MethodBase.GetCurrentMethod.Name will return the current
procedure name.
 
Does it also return who called that procedure at all?

Try:
Dim sf As StackFrame = New StackFrame(iii, True)
Dim mb As Reflection.MethodBase = sf.GetMethod
where StackFrame is defined in System.Diagnostics, and iii is the call
ancestry (0 = current sub/function, 1 = caller, 2 = caller's caller, etc).
Some useful display strings are:
mb.DeclaringType().Name()
mb.Name
In a debug build, additional useful display strings are:
sf.GetFileName
CStr(sf.GetFileLineNumber)
 
You could also use System.Diagnostics.StackTrace. It surprised me though in
the following code, the release mode version apparently optimized out
MethodOne and went directly to MethodTwo. This oddity was in Orcas, so I'd
be interested if it is not optimized out in VS 2005.

class Program
{
private static void MethodOne()
{
MethodTwo();
}

private static void MethodTwo()
{
System.Diagnostics.StackTrace st = new
System.Diagnostics.StackTrace();

Console.Out.WriteLine(st.ToString());
System.Threading.Thread.Sleep(5000);
}

static void Main(string[] args)
{
MethodOne();
}
}
 
Oops! Sorry about the incorrect language, but the VB should be similar!

ModelBuilder said:
You could also use System.Diagnostics.StackTrace. It surprised me though in
the following code, the release mode version apparently optimized out
MethodOne and went directly to MethodTwo. This oddity was in Orcas, so I'd
be interested if it is not optimized out in VS 2005.

class Program
{
private static void MethodOne()
{
MethodTwo();
}

private static void MethodTwo()
{
System.Diagnostics.StackTrace st = new
System.Diagnostics.StackTrace();

Console.Out.WriteLine(st.ToString());
System.Threading.Thread.Sleep(5000);
}

static void Main(string[] args)
{
MethodOne();
}
}


AMercer said:
Try:
Dim sf As StackFrame = New StackFrame(iii, True)
Dim mb As Reflection.MethodBase = sf.GetMethod
where StackFrame is defined in System.Diagnostics, and iii is the call
ancestry (0 = current sub/function, 1 = caller, 2 = caller's caller, etc).
Some useful display strings are:
mb.DeclaringType().Name()
mb.Name
In a debug build, additional useful display strings are:
sf.GetFileName
CStr(sf.GetFileLineNumber)
 
Brad said:
I am developing a simple error message class and would like the be able to
generate as part of the message the curent class/module/form and function or
sub that the error was generated in without having to hardcode this info to
be passed to the error message class.

System.Reflection.MethodBase.GetCurrentMethod().Name

gets you the name of the currently executing method, but you have to
code it into every method. Tedious.
I can get the stack trace using system.environment.stacktrace
- but this is too much info.

You /can/ get the StackTrace and pull it to pieces to extract the bit(s)
you're after. You might add this to the constructor (Sub New) of your
"error message class":

Dim sf As System.Diagnostics.StackFrame _
= New System.Diagnostics.StackFrame(1, False)
Dim method As System.Reflection.MethodBase _
= sf.GetMethod()

? method.ReflectedType.FullName
? method.Name
I just want to display the function/sub name where the error was generated
and the parent name of that sub/function

IMHO, if you're using this to track down errors, then you /need/ the
/entire/ stack trace (and more). The logging class I use gets laced
into each and every method and is used to record the entry and exit
points of those methods, complete with arguments and return values.
Without /all/ of this, it's .. rather difficult, shall we say .. to get
to the bottom of what's going wrong.

HTH,
Phill W.
 
Any chance of getting a copy of that logging class you use and some
instructions on it's implementation and use? Sounds like something I might
want to use... :-)

Thanks, Brad
 
Back
Top