Automatically generate a string of the namespace, class, function/procedure.

  • Thread starter Thread starter Mufasa
  • Start date Start date
M

Mufasa

Is there anyway to generate a string that contains the name of the
namespace, the object name and then the function/procedure name that is
currently running.

I want to use this for logging information and am getting tired of having to
type/cut-paste the things.

If I can't get it all, can I get at least some of it?

TIA - Jeff.
 
Mufasa said:
Is there anyway to generate a string that contains the name of the
namespace, the object name and then the function/procedure name that is
currently running.

You can use the StackTrace class to get the last StackFrame in the
stack. This has a GetMethod() method which will give you the information
that you want.

Note, however, that this is not a fast operation, so you will not want
to leave this permanently active inside your methods.
 
Is there anyway to generate a string that contains the name of the
namespace, the object name and then the function/procedure name that is
currently running.

I want to use this for logging information and am getting tired of having to
type/cut-paste the things.

If I can't get it all, can I get at least some of it?

TIA - Jeff.

Here is a C# example:
static void Main(string[] args)
{
System.Diagnostics.StackFrame sf = new
System.Diagnostics.StackFrame(true);
string fileName = sf.GetFileName();
string lineNumber = sf.GetFileLineNumber().ToString();
string methodName = sf.GetMethod().Name;

string output = string.Format("File name={0} , Line
number={1} , Method name={2}", fileName, lineNumber, methodName);
Console.WriteLine(output);
}
 
Mufasa said:
Is there anyway to generate a string that contains the name of the
namespace, the object name and then the function/procedure name that is
currently running.

Take a look at the thread titled "Identifying function name" started in this
newsgroup by "AA2e72E" on 2007/09/07

There is a discussion on using StackTrace and System.Reflection.Methodbase

Regards,
Nicolas
 

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