How to use wrap handler to include custom information in message?

Topics: Exception Handling Application Block, Logging Application Block
Apr 21, 2008 at 11:40 PM
Hi all,

I am using entlib may 2007 release exception handling and logging application block. so far i was using logging to log exception details in db, flat file, xml and event log. Since the message property of exception is read-only, I think I need to use the wrap handler. With this wrap handler i should be able to add more information in my message( leaving the actual information in the inner exception).

Is this the correct approach?
Is there another way (probably easier) to do the same?
How do i set up wrap handler exception message property programmatically?


Also, how do i use both wrap handler and logging handler at the same time? Is there any consideration of sequence and or change in the code?

I would appriciate if you guys can give me some sample code.

Thank you for your help in advance.

Apr 24, 2008 at 12:56 PM
Hi,

If you need to set messages programmatically you'll need a custom handler. Here's a fine example from Tom Hollander showing how to translate SQL error codes into exception messages http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx. It will show you how to write such a custom handler.

You can use many handlers, and each handler will receive the result of the previous handler's processing. The configuration tool will let you rearrange the handlers to get the desired sequence. Application code should not need changes; in fact that's the purpose of the EHAB.

Cheers,
Fernando
Apr 27, 2008 at 8:10 PM
Thank you. The link you've provided is useful. However, my problem here is to add variable values in the messages. For example in customer order system, if one of the order method threw an exception and Exception block is configured to send an email notification along with other things such as logging in event viewer, it will be helpful for the email recepient to have order ID as part of the exception email. In my case, the method is looping through all order records and the requirement is to continue looping with other order records even if the for particular order record, it threw an exception. I wrote my code something like this.


try
{

......
dosomething();
.......
}
catch(SqlException sqlEx)
{
try{
throw new DataException(sqlEx.message + System.Environment.Newline + "The order ID : " + ordID);
}
catch(DataException dEx)
{
ExceptionPolicy.HandleException(dEx, "Logging Policy");
}
}

SqlException is not configured in Exception handling configuration block. However, DataException is to log the error and send an email. The caller of this method calls it in a loop and this code prevents the application to stop in case of exception occur in one of the intermediate record and also it will send the ordID as part of the email to all the recipient configured through email trace listner.

Even though this solution is working, I do not like the idea of writing this boiler plate 4 line code in catch block in each method. Since this is a stand alone application, I do not feel like writing my custom exception class and add to the EntLib as this will not be used in other applications(at least for now).




fsimonazzi wrote:
Hi,

If you need to set messages programmatically you'll need a custom handler. Here's a fine example from Tom Hollander showing how to translate SQL error codes into exception messages http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx. It will show you how to write such a custom handler.

You can use many handlers, and each handler will receive the result of the previous handler's processing. The configuration tool will let you rearrange the handlers to get the desired sequence. Application code should not need changes; in fact that's the purpose of the EHAB.

Cheers,
Fernando

Apr 29, 2008 at 2:02 PM
Why don't you just put the new DataException() code inside the ExceptionPolicy.HandleException:
if (ExceptionPolicy.HandleException(new DataException(sqlEx.message + System.Environment.Newline + "The order ID : " + ordID), "Logging Policy") throw;

And if DataException is your own exception class, use typed params instead of concatenating a string in the c'tor.
Apr 29, 2008 at 4:57 PM
Thank you..This is very helpful.

KjellSJ wrote:
Why don't you just put the new DataException() code inside the ExceptionPolicy.HandleException:
if (ExceptionPolicy.HandleException(new DataException(sqlEx.message + System.Environment.Newline + "The order ID : " + ordID), "Logging Policy") throw;

And if DataException is your own exception class, use typed params instead of concatenating a string in the c'tor.