The design and implementation of a poll-type error-handling mechanism (III.)

Source: Internet
Author: User
Tags constructor error code error handling hash mail system log tostring mailmessage
Error | Error handling | design Errhdleventlogclass:



This class is the concrete implementation class that writes error information to the system log, which inherits from Abstracthandleclass. Users can customize the error-handling method by imitating the class. The specific code is as follows:



public class Errhdleventlogclass:abstracthandleclass

{

public const int ID = 2; Number of error-handling methods



Public Errhdleventlogclass ()

{

//

Todo:add constructor Logic here

//

}



Overloaded the method of the parent class to implement the processing action of the error message

public override int HandleError (Errormsgclass emsg)

{

if (this. Handleflag (Emsg,id))//judge whether to handle

{

Try

{

EventLog el = new EventLog ();

El. Source = emsg.hash["Eventlogsource"]. ToString ();

El. WriteEntry (FMT. Format (emsg), EventLogEntryType.Error);

return 0;

}

Catch

{

return 1; Processing failed

}

}

Else

{

return 9999; The user did not select this method of processing

}

}

}






Errhdlmailclass:



This class is a concrete implementation class that sends error messages through mail. The specific code is as follows:



public class Errhdlmailclass:abstracthandleclass

{

public const int ID = 5;



Public Errhdlmailclass ()

{

//

Todo:add constructor Logic here

//

}



public override int HandleError (Errormsgclass emsg)

{

if (this. Handleflag (Emsg,id))

{

Try

{

MailMessage mail = new MailMessage ();

Mail. from = emsg.hash["Mailfrom"]. ToString ();

Mail. to = emsg.hash["MailTo"]. ToString ();

Mail. Subject = emsg.hash["Mailsubject"]. ToString ();

Mail. BodyFormat = Mailformat.text;

Mail. BODY = FMT. Format (EMSG);



Smtpmail.send (mail);



return 0;

}

Catch

{

return 1;

}

}

Else

{

return 9999;

}

}

}





--------------------------------------------------------------------------------

How the client is invoked

The following is the implementation code that invokes the error handling mechanism in the user's application:

Errorhandleclass err = Errorhandleclass.getsingleton ();//Get an instance of the error-handling class

Errormsgclass emsg = new Errormsgclass (); Create an instance of this error message

EMSG.HASH.ADD ("Mailfrom", "lu224@oki.com"); Add mailfrom Information

EMSG.HASH.ADD ("MailTo", "lu224@oki.com"); Add mailto information

EMSG.HASH.ADD ("Mailsubject", "A test!");

emsg.hash["Assembly"] = "AssemblyName"; Name of the component where the error occurred

emsg.hash["Class"] = "ClassName"; Name of the class where the error occurred

emsg.hash["errorcode"] = 1112; Error code

emsg.hash["errormessage"] = "Error message"; Error message

emsg.hash["Function"] = "functionname"; The name of the function where the error occurred

emsg.hash["Mode"] = Global.mail; Set the error handling method

int t = Err. HandleError (EMSG); Performing error handling





--------------------------------------------------------------------------------

Summarize

This paper introduces an implementation model of error handling mechanism in polling mode. The advantage of this mechanism is that two aspects of error handling can be customized through inheritance. One is the method of error handling, common methods are to write system log, send mail, write log file, pop-up error message box, write database, etc. The mechanism can be designed in advance to allow users to choose (multiple choices), if the user wants the method is not defined, the user must inherit Abstracthandleclass, you can design the wrong way of handling. The second is the format of the error message, if the user is not accustomed to the default error message output format, then inherit Abstractmessageformatclass, you can customize the error message output format and content.



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.