Get class- and functionname

S

sewid

Hi!

Is there a possibility to get the class- and the functionname from the
current function? I want to log all exceptions with the class and the
function, where the exception occured. So I created a function
logException(string message, string classname, string functionname)
and it would be really cool to fill the classname and functionname
automatically.

Best regards,
Sebastian
 
J

Jon Skeet [C# MVP]

Is there a possibility to get the class- and the functionname from the
current function? I want to log all exceptions with the class and the
function, where the exception occured. So I created a function
logException(string message, string classname, string functionname)
and it would be really cool to fill the classname and functionname
automatically.

See MethodBase.GetCurrentMethod.

Jon
 
J

Jon Skeet [C# MVP]

Is there a possibility to get the class- and the functionname from the
current function? I want to log all exceptions with the class and the
function, where the exception occured. So I created a function
logException(string message, string classname, string functionname)
and it would be really cool to fill the classname and functionname
automatically.

Scratch previous response - if you call it from LogException, it'll
return LogException :) You probably want the StackTrace class.

Note that other people have already done a lot of this work for you -
look at the log4net framework.

Jon
 
L

Lasse Vågsæther Karlsen

Jon said:
Scratch previous response - if you call it from LogException, it'll
return LogException :) You probably want the StackTrace class.

Note that other people have already done a lot of this work for you -
look at the log4net framework.

Jon

If he's logging exceptions, isn't what he want already provided in the
stack trace of the exception object?

In other words, if he attempts to use StackTrace or similar, he will be
logging where the logging occured, not where the exception occured.

Or am I missing something? (I bet I am)
 
L

Lasse Vågsæther Karlsen

Jon said:
Scratch previous response - if you call it from LogException, it'll
return LogException :) You probably want the StackTrace class.

Note that other people have already done a lot of this work for you -
look at the log4net framework.

Jon

If he's logging exceptions, isn't what he want already provided in the
stack trace of the exception object?

In other words, if he attempts to use StackTrace or similar, he will be
logging where the logging occured, not where the exception occured.

Or am I missing something? (I bet I am)
 
L

Lasse Vågsæther Karlsen

Jon said:
Scratch previous response - if you call it from LogException, it'll
return LogException :) You probably want the StackTrace class.

Note that other people have already done a lot of this work for you -
look at the log4net framework.

Jon

If he's logging exceptions, isn't what he want already provided in the
stack trace of the exception object?

In other words, if he attempts to use StackTrace or similar, he will be
logging where the logging occured, not where the exception occured.

Or am I missing something? (I bet I am)
 
J

Jon Skeet [C# MVP]

Or am I missing something? (I bet I am)

Nope, not really - it was me missing that :)

Unless of course he wants to log where it was caught as well as where
it was thrown.

Jon
 
L

Lasse Vågsæther Karlsen

Lasse said:
If he's logging exceptions, isn't what he want already provided in the
stack trace of the exception object?

In other words, if he attempts to use StackTrace or similar, he will be
logging where the logging occured, not where the exception occured.

Or am I missing something? (I bet I am)

Sorry about the multiple replies. I had inadvertantly hit "Reply all",
which included Jon's email in the to-list, and my email server
apparently doesn't like Thunderbird, so I hit send 2 times before I
noticed :)

(Hope I didn't spam Jon in the process)
 

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