Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine current method

Status
Not open for further replies.

chiph

Programmer
Jun 9, 1999
9,878
US
In my error logging, I'd like to be able to determine the method that caused an error.

I've been all through the System.Diagnostics and System.Reflection namespaces, and I haven't spotted anything that would allow me to do this ... it must be there, but I'm just not seeing it.

So far the best idea I've come up with is to get a Stacktrace, and the look at the first few Frames in it. This will probably work, but seems like a kludge.

Thanks in advance.

Chip H.
 
Chip,
I'm not sure if this will work with C#, but it does fine in other languages I've used: why not have a log file that writes all trapped errors? Then you could pass all the info you'd like to see to the log file for debugging and/or troubleshooting. Kinda simple but effective :)

BKtechie
 
My purpose is not to have to type in the method name in every single call to my error logging routine. I don't want to have to do this:
Code:
WriteMyError("ThisMethod", Errorinfo, stackinfo, ErrorDescription);
I'd much rather do something like:
Code:
WriteMyError(this, Errorinfo, stackinfo, ErrorDescription);
where I could extract the current method name from the "this" reference. What usually happens is I cut & paste the same code a number of times and I forget to update that hard-coded method name. So I get errors saying they're coming from Method_A when they really originated in Method_B.

Chip H.
 
I'm thinking stack trace would be the only way. Make a function that strips out the text that wouldn't be a method name.
 
I think I just haven't found out how to do it yet - if you notice in the IDE when you perform a search, one of the region options is the current method - *it* knows about the current method name, so I ought to be able to get it too.

Chip H.
 
what environment are you using chip? i use vs.net. when i get an error i get the entire method stack in the console window, with line numbers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top