Get Current Class/Function Name

D

DrewCE

As you've discovered you don't have runtime access to the StackFrame unless
you throw an exception and parse the CallStack string.

However, the class name can be retrieved by doing something like this....

this.GetType().ToString();

If the info is only needed during debug sessions, then use a conditional
break point and log to the Output window as follows...

Set a break point where you want a log message to be created.
Right click on the break point.
Chose "When hit..." form the popup menu.
Check "Print a message"

The default message format includes the method name, thread ID, and thread
name. I've found the $CALLSTACK variable to be very handy at times.

-Drew
 
J

Jayesh

As you've discovered you don't have runtime access to the StackFrame unless
you throw an exception and parse the CallStack string.

However, the class name can be retrieved by doing something like this....

this.GetType().ToString();

If the info is only needed during debug sessions, then use a conditional
break point and log to the Output window as follows...

Set a break point where you want a log message to be created.
Right click on the break point.
Chose "When hit..." form the popup menu.
Check "Print a message"

The default message format includes the method name, thread ID, and thread
name. I've found the $CALLSTACK variable to be very handy at times.

-Drew

Hi,

Looking at the type at run time is not good thing for compact
framework.
The call to GetType is not probably worth for this kind of things.
You might want to consider keeping the name of class in with some
string variables.
Create a generic logger just to write in your log file or use can use
Log4Net.
Class A
{
static string className = "A";

public int Add(int a, int b)
{
if(EnabledTrace)
{
loggger.log("Entering Class : A, method:Add");
}

}
}

This might be better way.
Let me know if this works out.

Thanks,
Jayesh Modha
 
S

stealthrabbi

Jayesh,

This works, but this doesn't solve my problem of not knowing the class/
function name at run time. i'd have to copy/paste that wherever i want
to log and change the literal strings.
 

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

Top