For basic knowledge, see http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stacktrace.aspx
Basic instance see: http://www.cnblogs.com/isabelincoln/archive/2006/11/02/547544.html thisArticleTwo references must be added before the operation can be successful.
Using system. diagnostics;
Using system. reflection; // reflection
Call getframe to obtain the stack space. The index parameter indicates the stack space level. 0 indicates the current stack space. 1 indicates the stack space at the upper level, and so on.
Besides obtaining method information, you can also call the member functions of the stackframe class to obtainCodeFile Information, row numbers, column numbers, etc.
Example:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. diagnostics; using system. reflection; namespace consoletest {class program {static void main (string [] ARGs) {myinternalclass myclass = new myinternalclass (); myclass. mypublicmethod () ;}} class myinternalclass {public void throwsexception () {try {Throw new exception ("error message");} catch {stacktrace ST = new stacktrace (true ); string stackindent = ""; for (INT I = 0; I <St. framecount; I ++) {stackframe Sf = ST. getframe (I); console. writeline (); // obtain the error method console. writeline (stackindent + "method: {0}", SF. getmethod (); // get the error file name console. writeline (stackindent + "file: {0}", SF. getfilename (); // get the console with the wrong row number. writeline (stackindent + "line number: {0}", SF. getfilelinenumber (); // get the error column console. writeline ("column number: {0}", SF. getfilecolumnnumber (); stackindent + = "" ;}} protected void myprotectedmethod () {myinternalclass MIC = new myinternalclass (); mic. throwsexception ();} public void mypublicmethod () {myprotectedmethod ();}}}
Result
We can use log4net to write an interface for Error Log prompts!