Trace/Logging Question

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

Guest

Hi,

I would like to output the source code module name and line number in some of my Trace.Writeline() calls. The information that I need is in the Environment.StackTrace string however I DO NOT want to issue a full stack trace and then parse a string every time I wish to Trace source code info!!!!!!!! For you ex-C++ now C# coders I wanna do something like this:

Trace.WriteLine(__FILE__ + "-" + __LINE__ + ": " + msg);

Is there a better way to get this info - say through reflection?? - without issuing a full stack trace every time I log information?

For extra credit does anybody know how to get the class & method name without issuing a full stack trace/parse????

--Richard
 
Richard,

This will get you the current method and line number....

System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace();
System.Diagnostics.StackFrame frame = stack.GetFrame(0);
Console.WriteLine("method name: " + frame.GetMethod().Name );
Console.WriteLine("line number: " + frame.GetFileLineNumber() );

Hope this helps,

~harris
 
Here is another possibility...

string theGoodStuff = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName.ToString();

good luck,

~harris
 

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