Create an exceptionlog by yourself

Source: Internet
Author: User

After Reading Li-New's new article and getting detailed information about system exceptions, I thought of dynamicproxy and aop I played a few days ago. If I combined them, isn't it a custom exception record system?
Well, just do it.

Here, I didn't use jgtm's method of using realproxy, because I have already understood it in dynamic for. net, isn't it :)

First, write an interface.
Public interface icalculator {
Void run ();
}
Then implement our class.
Public class calculator: icalculator {
Public void run (){
Console. writeline ("hell, this is the run ");
}
}

Okay. Now we have to write the class that records the exception information...
To record exception information, there must be an exception or its subclass. Well, good ~, Let's write an interface (why do we need to write an interface? Isn't it much convenient to write classes directly? Why is it so troublesome ?)
Public interface ilog {
Object logexception (exception ex );
Void write (Object res );
}

Implement this interface:
Public class log: ilog
{
# Region ilog members

Public object logexception (exception ex)
{
// Todo: Add log. Write implementation
// The code for this location is taken from the detailed information for obtaining exception information
Stacktrace trace = new stacktrace (ex, true );
Stackframe SF;
String res = "";
Memberinfo Mi;
For (INT I = 0; I <trace. framecount; I ++ ){
Sf = trace. getframe (I );
Mi = SF. getmethod ();
Res = mi. declaringtype. namespace + ".";
// Class Name
Res = res + MI. declaringtype. Name + ".";
Res = res + MI. Name;
// Obtain the file name (physical path), row number, and column number.
If (SF. getfilename ()! = String. Empty ){
Res = res + SF. getfilename () + ", line" + SF. getfilelinenumber () + ", col" + SF. getfilecolumnnumber ();
}
}
Write (RES );
Return res;
}

Public void write (Object res ){
Console. writeline (RES );
}

# Endregion

}
Here we can see that logexception (...) the function is used to obtain information from the exception, write (...) the function is used to display the exception information through the command line :)
Besides, here I call write directly in logexception ..

Well, the log of the exception class has been written. Now it's time to write it in the logfactory class: P
Similarly, our logfactory inherits from dynamicproxy. iproxyinvocationhandler (don't ask me why, you will understand the dynamicproxy code)
Public class logfactory: dynamicproxy. iproxyinvocationhandler
Then Constructor
Because we want to use the log class to record exception information, the architecture function is like this:
Private object target;
Private ilog log;

Public logfactory (Object OBJ ){
Target = OBJ;
}

Public logfactory (Object OBJ, ilog log ){
Target = OBJ;
This. log = log;
}

Then implement the function to generate a dynamic Proxy:

Public object create (){
Return proxyfactory. getinstance (). Create (this, target. GetType ());
}
This is where the function is called:

Public object invoke (Object proxy, system. reflection. methodinfo method, object [] parameters ){
// Todo: Add logfactory. Invoke implementation
Try {
Object result = method. Invoke (target, parameters );
Return result;
}
Catch (exception ex ){
If (log! = NULL) {// judge whether the log class is not null. If it is null, no record is required: P
Log. logexception (Ex );
}
Return NULL;
}
}

All the writes are written ~ Then the test case is: P
// No exception needs to be recorded here
[Test] public void testrun (){
Icalculator Cal = (icalculator) (new logfactory (New calculator (). Create ());
Cal. Run ();
}

// Abnormal information is recorded here

[Test] public void testlogrun (){
Icalculator Cal = (icalculator) (new logfactory (New calculator (), new log (). Create ());
Cal. Run ();
}

Okay... so we have completed a simple exception record system (let's just say )...
Haha... we can further write our xmllog class, record the exception information to an XML file, or write another envenlog class, and put the exception information into the system event: P


Source code download: exceptionlog

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.