用于在C#中调试db相关方法的更好的错误处理程序代码?

时间:2009-05-06 02:05:11

标签: c# database debugging error-handling

所有。我有以下区域,我在try {}块之后继续进行代码拦截。它的目的是在配置回数据库时提供调试信息...它可以很好地调试(不是所有它应该调试,但足以找到确切的错误,但是......

代码闻起来。即使我能得到它。您是否更好地了解了db方法的错误处理(我目前正在使用log4net实现)

以下是代码:

region CatchExceptionsAdv5NoReturnAtAll

    catch (NullReferenceException nre)
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += nre.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch

    catch (System.InvalidOperationException ioe) //comm -- occurs when no result set was found !!!
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ioe.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch (System.InvalidOperationException)

    catch (System.IndexOutOfRangeException ioore) //comm -- occurs when no result set was found !!!
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ioore.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch (System.IndexOutOfRangeException)

    catch (System.Data.SqlClient.SqlException sqle)
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += sqle.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch

    catch (System.FormatException fe)
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += fe.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch

    catch (Exception ex)
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch
    #endregion CatchExceptionsAdv5NoReturnAtAll

2 个答案:

答案 0 :(得分:2)

你应该看一下exception handling block in Enterprise Library。它可以让你做很多你想做的事,而不需要重复这么大的代码块。但是,一般来说,为什么不分解每个异常所包含的所有重复代码。至少,如果您不想使用EntLib,请将所有重复的代码分解出来并构建一个方法库,您可以从异常处理块中干净地调用它们来执行您想要执行的操作。

答案 1 :(得分:0)

我认为真正的答案可能是使用Enterprise Library或log4net ......同时,快速而肮脏的重构方式是用以下调用替换该区域:

  catch (Exception ex)
  {
    System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
    Utils.ErrorHandler.Trap ( ref userObj, st, ex );
    return false;
  } //eof catch

指向了错误处理程序类,它没有改进错误报告,也没有提供更好的错误报告体系结构......到目前为止它只删除了重复的代码并提高了可读性:

using System;

namespace Utils
{
  public class ErrorHandler
  {


    public static void Trap ( ref FB.User userObj, System.Diagnostics.StackTrace st, Exception ex )
    {
      if (userObj == null)
      {
        userObj = new FB.User ();
        FB.User.GiveDefaultUser ( ref userObj );
      }


      if (ex is NullReferenceException)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.InvalidOperationException) //comm -- occurs when no result set was found !!!
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.IndexOutOfRangeException) //comm -- occurs when no result set was found !!!
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.Data.SqlClient.SqlException)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.FormatException)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is Exception)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof catch


    } //eof method 



  }//eof class CustomException
} //eof namespace U