views:

45

answers:

1

Scenario:

I've recently learned about Mark Russinovich's useful DebugView* utility.
With DebugView, even with vs2008 closed, I can use statements like this:

System.Diagnostics.Debug.WriteLine("some text =. " + some_variable);  

I would like to modify the above, at compile time, to include the source code line number:

System.Diagnostics.Debug.WriteLine("nnnn: some text =. " + some_variable);  

where nnnn represents the source code line number.

AFAIK, to do this, vs2008 would need some form of meta variable like @linenumber for current compile time source code line number. i.e., if the Debug.Writeline statement
is on the tenth line, then such a meta-variable would == 10.

Thus, a debug statement such as the rough example above could be used in multiple places and it would be easy to identify the source code location of each debugging statement.

QUESTION: are there meta variables that can be referenced at compile time?

P.S.: I could not find the answer to this question via Google and also via SO search feature.

A: 

Even though I hope there is a method to obtain this information faster, here's a way you could get the file name and line number using the StackTrace class in the System.Diagnostics namespace:

StackTrace st = new StackTrace(new StackFrame(true));
string fileName = st.GetFrame(0).GetFileName();
string lineNumber = st.GetFrame(0).GetFileLineNumber().ToString();
string fileNameAndLineNbr = fileName + ":" + lineNumber;

System.Diagnostics.Debug.WriteLine(fileNameAndLineNbr + ": some text =. " + some_variable);
Miky Dinescu