击鼓传花——轮询式错误处理机制的设计与实现(三)

类别:.NET开发 点击:0 评论:0 推荐:

ErrHdlEventLogClass

 

              该类是将错误信息写入系统日志的具体实现类,该类继承自AbstractHandleClass。用户在自定义错误处理方式的时候,便可仿照该类来设计。具体代码如下:

 

public class ErrHdlEventLogClass: AbstractHandleClass

{

                            public const int ID = 2;                         //错误处理方式的序号

 

                            public ErrHdlEventLogClass()

                            {

                                          //

                                          // TODO: Add constructor logic here

                                          //

                            }

 

         //重载了父类的方法,具体实现对错误信息的处理动作

                            public override int HandleError(ErrorMsgClass emsg)

                            {

                                          if(this.HandleFlag(emsg,ID))                      //判断是否进行处理

                                          {

                                                        try

                                                        {

                                                                      EventLog el = new EventLog();

                                                                      el.Source = emsg.hash["EventLogSource"].ToString();

                                                                      el.WriteEntry(fmt.Format(emsg),EventLogEntryType.Error);

                                                                      return 0;

                                                        }

                                                        catch

                                                        {

                                                                      return 1;                  //处理失败

                                                        }

                                          }

                                          else

                                          {

                                                        return 9999;                   //用户未选择该种处理方式

                                          }

                            }

}

 

 

ErrHdlMailClass

 

              该类是将错误信息通过邮件发送的具体实现类。具体代码如下:

 

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;

                                          }

                            }

}

 


客户端调用方式

              以下是在用户的Application中调用该错误处理机制的实现代码:

ErrorHandleClass err = ErrorHandleClass.GetSingleton();//获得错误处理类的实例

ErrorMsgClass emsg = new ErrorMsgClass();            //创建该错误信息的实例

emsg.hash.Add("MailFrom","[email protected]");          //添加MailFrom信息

emsg.hash.Add("MailTo","[email protected]");             //添加MailTo信息

emsg.hash.Add("MailSubject","A Test!");

emsg.hash["Assembly"] = "AssemblyName";            //错误所在的组件名称

emsg.hash["Class"] = "ClassName";                     //错误所在的类名

emsg.hash["ErrorCode"] = 1112;                        //错误代码

emsg.hash["ErrorMessage"] = “Error message”;         //错误信息

emsg.hash["Function"] = "FunctionName";              //错误所在的函数名

emsg.hash["Mode"] = Global.Mail;                      //设置错误处理方式

int t = err.HandleError(emsg);                           //执行错误处理

 


总结

本文介绍了一种轮询方式的错误处理机制的实现模型。该机制的优点是可以对错误处理的两方面通过继承进行定制。一是错误处理的方法,常见的方法有写入系统日志、发送邮件、写入日志文件、弹出错误信息框、写入数据库等等。该机制可以将这些方法都预先设计好,让用户进行选择(可进行多重选择),假如用户希望的方法不在定义中,用户只须继承AbstractHandleClass,即可自己来设计错误的处理方式。二是错误信息的格式,用户如果不习惯默认的错误信息输出格式,那么继承AbstractMessageFormatClass,即可自己定制错误信息的输出格式和内容。


更多信息

 


作者:陆

2004411

本文地址:http://com.8s8s.com/it/it44838.htm