Exception Handling Block - OracleException - Best Practice for commandText RRS feed

  • Question

  • User681754118 posted

    Hello.  I'm completely redesigning the architecture of our web app, and am now first determining how my exception handling will be implemented.  I decided on using the Exception Handling Block 4.0 and I am new to this block ( I have used the data access and logging before). 

    Just performing some simple best practices tests right now, just logging the exception and rethrowing.  I'm trying to determine how I want to handle the exceptions in my data access layer and how to get all the relevant data access information logged.  I want to make sure the commandText and parameters are included in the logging.  We're using Oracle and System.Data.OracleClient objects.  I'm looking for a best practice or even a good article.  Thank you for any help you can provide.

    Wednesday, September 17, 2008 1:07 PM

All replies

  • User-1040643152 posted

    Hi, here is a video about logging to databases using log4net. Check this link http://www.dimecasts.net/Casts/CastDetails/45

    Wednesday, September 17, 2008 1:48 PM
  • User681754118 posted

    I appreciate the reply, but I probably didn't explain myself well.  I am not trying to log to a database, what I am wanting is that if the exception is from my data acess, I want to log (in the event log, using the logging App Block) the relevant details about the data access that caused the exception using the Enterprise Library 4.0 exception handling block.

    1    OracleCommand command = new OracleCommand();
    2    try
    3    {
    4       OracleConnection connection = new OracleConnection(connstring);
    5       connection.Open();
    7       string commandText = "SELECT * FROM NonExistantTable WHERE column = :p1";
    8       command = new OracleCommand(commandText, connection);
    10      OracleParameter parameter = new OracleParameter("p1", OracleType.VarChar);
    11      parmeter.Value = "something";
    12      parameter.Direction = ParameterDirection.Input;
    14      command.Parameters.Add(parameter);
    15      command.ExecuteReader(CommandBehavior.CloseConnection);
    16   }
    17   catch (OracleException ex)
    18   {
    19      bool rethrow = ExceptionPolicy.HandleException(ex, "OracleException Policy");
    20      if (rethrow)
    21      {
    22          throw;
    23      }
    24   }

    So in the above example code, when the executeReader throws the exception of "ORA-00942: table or view does not exist", I want my log entry to not only contain the exception message, but also the command text and the details about the parameters of the command.

    Previously I've added those details to the Exception.Data collection then utilized them from there.

    1    catch (Exception ex)
    2    {
    3       ex.Data.Add("SQL", command.CommandText);
    5       // enumerate command Parameters collection
    6       ex.Data.Add("key", myKey);
    7       throw;
    8    }
    10   // ......  OR  ......
    12   catch (Exception ex)
    13   {
    14      ex.Data.Add(String.Format("{0}.{1}.SQL", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, System.Reflection.MethodBase.GetCurrentMethod().Name), command.CommandText);
    15      throw;
    16   }

    But I wasn't sure if there was a best practice or standard way to do this using the Exception Handling block.  Also wasn't sure if the best or only way to really do this was to create new Exception Handlers and / or Exception Formatters.

    That got long winded but hopefully I explained myself better this time.  I am sincerely sorry causing the confusion.


    Wednesday, September 17, 2008 3:11 PM