ASP. NET get the class name and Method Name of the current Code Location

Source: Internet
Author: User
// Test log protected void writeerror (Object sender, eventargs e) {string typename = This. getType (). tostring (); // when the class name is used // string methodname = new system. diagnostics. stacktrace (true ). getframe (1 ). getmethod (). declaringtype. tostring (); // This can be printed by the button call // string methodname = new system. diagnostics. stacktrace (true ). getframe (1 ). getmethod (). name; // event source, onclick, but the writeerror method name is not displayed .. String methodname = system. reflection. methodbase. getcurrentmethod (). Name ;//ProgramExecution locationCodeBlock Method Name // response. write (typename + "," + methodname + "," + method + ","); string errmsg = "test err message"; log. write (enums. logtype. error, errmsg, "type name:" + typename, "method name:" + methodname );}

Directly run the code, that is, the current GetType () can be used for the current class name. In addition, the system. Diagnostics. stacktrace and system. reflection. methodbase functions greatlyArticleYou can dig. Go to msdn.

The current page is test. aspx. CS.

The output of the above three methodnames is as follows. Use them by yourself.

2010-09-29 16:30:23 test err message
Type name: Asp. test_aspx)
Method Name: system. Web. UI. webcontrols. Button

2010-09-29 16:30:52 test err message
Type name: Asp. test_aspx
Method Name: onclick

2010-09-29 16:31:11 test err message
Type name: Asp. test_aspx

Method Name: writeerror (Exactly What I Want)

Related Article

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.