vs2008 can I get value of line number @ *?
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.
- http://technet.开发者_如何学JAVAmicrosoft.com/en-us/sysinternals/bb896647.aspx "DebugView for Windows v4.76", Mark Russinovich; October 16, 2008
this is the best way I know to get the ACTUAL line CORRECTLY (i.e. it's the right line because it's in-line)
Debug.WriteLine(" hi there I am line number [" + (new StackTrace(new StackFrame(true)).GetFrame(0).GetFileLineNumber().ToString()) + "]");
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);
精彩评论