473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

determining module/class and function/subroutine currently being run

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
Jul 12 '07 #1
7 3037
"Brad Pears" <br***@truenorthloghomes.comwrote in
news:Og**************@TK2MSFTNGP04.phx.gbl:
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.
Jul 12 '07 #2
Does it also return who called that procedure at all?

Brad
"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
"Brad Pears" <br***@truenorthloghomes.comwrote in
news:Og**************@TK2MSFTNGP04.phx.gbl:
>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.

Jul 12 '07 #3
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)

Jul 12 '07 #4
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" wrote:
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)
Jul 13 '07 #5
Oops! Sorry about the incorrect language, but the VB should be similar!

"ModelBuilder" wrote:
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" wrote:
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)
Jul 13 '07 #6
Brad Pears wrote:
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().Na me

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.
Jul 16 '07 #7
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

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:f7**********@south.jnrs.ja.net...
Brad Pears wrote:
>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().Na me

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.

Jul 27 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
7
by: Anonymous | last post by:
I have a mixed-language pgm (Fortran and C++) which needs to pass a few hundred values from C++ to Fortran. One way to do this is to have a Fortran module and a C++ structure (or class) with an...
1
by: Larry Bird | last post by:
I've created a AlertDataClass below within the class I have tables and column that I've create. In the AlertDataAccess class I'm trying to insert data into my tables. AlertDataAccess is a Module...
5
by: Larry Bird | last post by:
I've created a AlertDataClass below within the class I have tables and column that I've create. In the AlertDataAccess class I'm trying to insert data into my tables. AlertDataAccess is a Module...
9
by: craig.overton | last post by:
All, I am currently developing an FTP class in VB.NET. It's kid tested, mother approved when trying to access an FTP Server on a Windows box meaning I can connect, run commands, upload and...
21
by: Cottonwood | last post by:
I want to call a C module from a Fortran program. Whatever I tried - the linker could not find the C module. I know about the leading underscore and switched even that off. I abstracted everything...
10
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in...
1
by: Donald Westfield | last post by:
I have my own module, called mystuff.py, which is to be imported for an app. The number of classes in the module will vary, so I need a subroutine that will instantiate all of them as objects,...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.