Returning Function or Sub Name

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Like every developer, I've got tons of error trapping code that hard code
the name of the function or sub name so I can error log it. It seems from
other discussions that the only way to get the function or subprocedure name
is to either hard code it or to use a Constant to store it and reference the
Constant in each Catch statement. There are no properties that return the
function/sub name. Has anyone found a way thru reflection to get the name of
the currently running fuction/subprocedure name?

Thank you so much for your help.
 
John said:
Has anyone
found a way thru reflection to get the name of the currently running
fuction/subprocedure name?

System.Reflection.MethodBase.GetCurrentMethod.Name

HTH,
 
John said:
Like every developer, I've got tons of error trapping code that hard code
the name of the function or sub name so I can error log it. It seems from
other discussions that the only way to get the function or subprocedure name
is to either hard code it or to use a Constant to store it and reference the
Constant in each Catch statement. There are no properties that return the
function/sub name. Has anyone found a way thru reflection to get the name of
the currently running fuction/subprocedure name?

Thank you so much for your help.

The exception object contains a stack trace that contains the name of
the method, and also the methods that was called to get there.
 
John,
As Göran suggests, check out the Exception.StackTrace property.

http://msdn2.microsoft.com/en-us/library/system.exception.stacktrace.aspx
Like every developer, I've got tons of error trapping code that hard code
the name of the function or sub name so I can error log it.
Unlike VB6 & other COM based languages. .NET itself has all the error
trapping code to track the name of functions, subs, properties allowing you
to log rich Exception objects that include stack traces & other contextual
information; such as the Exception.Data collection:

http://msdn2.microsoft.com/en-us/library/system.exception.data.aspx

Has anyone found a way thru reflection to get the name of
the currently running fuction/subprocedure name?
Although you can use GetCurrentMethod.Name as (O)enone suggests. Simply
throwing & catch Exception objects avoids needing to know the name of
functions, subs, and properties. Further it avoids needing to code "tons of
error trapping code" to log it. I normally include "pounds, maybe ounces, of
error trapping code" at higher levels that record the entire Exception
object. At lower level, I may include a Try/Catch simply so the Catch can
include further contextual information (such as parameter values) in either
the Exception.Data property or custom properties on custom Exception classes
in the exception that is thrown to the higher levels.

Mostly my lower levels use Try/Finally or the new Using statement ensure
resources (IDisposable objects such as SQL Connection objects & System
Drawing/GDI+ objects) are property disposed of.
 
Back
Top