Introduction to C # stacktrace class instances

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.