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

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

 

ErrorHandleClass:

 

              该类是这个错误处理机制的核心,这是一个单例类。这个类创建了具体错误处理方式类的数组,并提供了HandleError方法,此方法对所有登记的错误处理方式进行轮询,达到根据用户的设置进行错误处理的目的。具体代码如下:

 

public class ErrorHandleClass

{

                            AbstractHandleClass[] _errHandleArray = new AbstractHandleClass [Global.ERROR_HANDLE_MODE];

                            private static ErrorHandleClass m_instance = null;

 

                            private ErrorHandleClass()

                            {

                                          //

                                          // TODO: Add constructor logic here

                                          _errHandleArray[0] = new ErrHdlDebugInfoClass();//本文不给出这个类的实现

                                          _errHandleArray[1] = new ErrHdlEventLogClass();

                                          _errHandleArray[2] = new ErrHdlMessageBoxClass();//本文不给出这个类的实现

                                          _errHandleArray[3] = new ErrHdlLogFileClass();//本文不给出这个类的实现

                                          _errHandleArray[4] = new ErrHdlMailClass();

                                          //

                            }

 

                            public static ErrorHandleClass GetSingleton()

                            {

                                          if(m_instance == null)

                                          {

                                                        m_instance = new ErrorHandleClass();

                                          }

                                          return m_instance;

                            }

 

                            public int HandleError(ErrorMsgClass emsg)

                            {

                                          int result = 0;

                                          for(int i=0;i<_errHandleArray.Length;i++)

                                          {

                                                        int t = _errHandleArray[i].HandleError(emsg);

                                                        if(t == 9999)

                                                        {

                                                                      continue;

                                                        }

                                                        else

                                                        {

                                                                      result += t;

                                                        }

                                          }

                                          return result;

                            }

}

 

AbstractHandleClass:

 

              该抽象类是所有错误处理方式的具体实现类的父类。每个具体的错误处理类重载该类的HandleError方法达到错误处理方式多样化的目的。该类的HandleFlag方法是轮询方式实现的核心,每个子类调用该方法来判断是否需处理这个错误信息。具体代码如下:

 

public abstract class AbstractHandleClass

{

                  protected AbstractMessageFormatClass fmt = new MessageFormatClassA();

 

                            public AbstractHandleClass ()

                            {

                                          //

                                          // TODO: Add constructor logic here

                                          //

                            }

 

                            /// <summary>

                            /// Handle error message.

                            /// </summary>

                            public virtual int HandleError(ErrorMsgClass emsg)

                            {

                                          return 0;

                            }

 

                            /// <summary>

                            /// A flag to decide handle the error or not.

                            /// </summary>

                            protected bool HandleFlag(ErrorMsgClass emsg, int id)

                            {

                                          int iMode = (int)emsg.hash["Mode"];

                                          string iCode = Global.NumberSplit(iMode);

                                          string flag = "0";

                                          if(iCode.Length>=id)

                                          {

                                                        flag = iCode.Substring(iCode.Length-id,1);

                                          }

                                          if(flag.Equals("1"))

                                          {

                                                        return true;

                                          }

                                          else

                                          {

                                                        return false;

                                          }

                            }

 

}

 

AbstractMessageFormatClass:

 

该类是定义错误信息输出格式的抽象类,子类继承该类并重载Format方法来重新定义输出格式。该类的Format方法是对错误信息输出格式的默认定义。具体代码如下:

 

public abstract class AbstractMessageFormatClass

{

                            public AbstractMessageFormatClass()

                            {

                                          //

                                          // TODO: Add constructor logic here

                                          //

                            }

 

                            /// <summary>

                            /// Define the output format of error message.

                            /// </summary>

                            public virtual string Format(ErrorMsgClass emsg)

                            {

                                          string msg = "";

                                          char enter = (char)13;

                                          msg += "------error message------" + enter.ToString();

                                          msg += "Assembly Name: " + emsg.hash["Assembly"].ToString() + enter.ToString();

                                          msg += "Class Name: " + emsg.hash["Class"].ToString() + enter.ToString();

                                          msg += "Function Name: " + emsg.hash["Function"].ToString() + enter.ToString();

                                          msg += "DateTime: " + emsg.hash["DateTime"].ToString() + enter.ToString();

                                          msg += "Error Code: " + emsg.hash["ErrorCode"].ToString() + enter.ToString();

                                          msg += "Error Message: " + emsg.hash["ErrorMessage"].ToString() + enter.ToString();

                                          msg += "-------------------------" + enter.ToString();

 

                                          return msg;

                            }

 

}

 

MessageFormatClassA

 

              该类用来重载Format方法,它继承AbstractMessageFormatClass类,对错误信息输出格式进行重新定义,用户也可自行创建MessageFormatClassB, MessageFormatClassC等类自由定义输出,只须继承AbstractMessageFormatClass类即可。具体代码如下:

 

public class MessageFormatClassA:AbstractMessageFormatClass

{

                            public MessageFormatClassA()

                            {

                                          //

                                          // TODO: Add constructor logic here

                                          //

                            }

 

         //以下为重载父类的Format方法,此处将其作为注释,不作使用

                            //public override string Format(ErrorMsgClass emsg)

                            //{

                            //           

                            //            return "";

                            //}

}

 

(未完待续)

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