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();
6
7 string commandText =
"SELECT * FROM NonExistantTable WHERE column = :p1";
8 command = new OracleCommand(commandText, connection);
9
10 OracleParameter parameter =
new OracleParameter("p1", OracleType.VarChar);
11 parmeter.Value =
"something";
12 parameter.Direction = ParameterDirection.Input;
13
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);
4
5 // enumerate command Parameters collection
6 ex.Data.Add("key", myKey);
7 throw;
8 }
9
10 // ...... OR ......
11
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 }
17
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.