Write exception to database problem

Topics: Exception Handling Application Block, Logging Application Block
May 30, 2007 at 7:44 PM
Hi all,
I am working with exception, logging and database block and meet following problem:
In code file I have
try
{
//statements cause exception
}
catch(Exception ex)
{
bool rethrow = ExceptionPolicy.HandleException(ex, "ToDBPolicy");
if (rethrow)
{
throw;
}
}
When exception occurs, everything run right, but no data is logged to database. But if I remove the "if(rethrow) throw", so data is logged right. The following code run right.

try
{
//statements cause exception
}
catch(Exception ex)
{
bool rethrow = ExceptionPolicy.HandleException(ex, "ToDBPolicy");
//if (rethrow)
//{
// throw;
//}
}

Some one have experience on this problem please help me!
May 31, 2007 at 4:54 PM
Assuming you are logging via a Logging Handler in the Exception Policy, I don't know why you would have a problem, The logging should be happening before the rethrow, so the problem does not really make sense.

The QuickStarts in Enterprise Library show an example of this, so I would just double check the way you have your policy configured. This is a common structure and there are no known problems.

Regards,

Dave

______________________________

David Hayden
Microsoft MVP C#
Jun 1, 2007 at 3:34 AM
I also don't know why it happens. When I do debuging, the code write to DB lob finishes but not effect to database. Be sure that logging works well and in case remove if (rethrow) throw the exception is logged to database well.
QuickStarts has not example of logging exception to DB.
Here is my config file:

<loggingConfiguration tracingEnabled="false" defaultCategory="toDB">
<categorySources>
<add switchValue="All" name="toDB">
<listeners>
<add name="Database" />
</listeners>
</add>
</categorySources>

<listeners>
<add databaseInstanceName="Northwind" writeLogStoredProcName="Log_WriteLog"
addCategoryStoredProcName="Log_AddCategory" formatter="DBFormatter"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database"
traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database"
name="Database" />
</listeners>

<formatters>
<add template="Message: {message}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
name="DBFormatter" />
</formatters>
</loggingConfiguration>

<exceptionHandling>
<exceptionPolicies>
<add name="Server Global Policy">
<exceptionTypes>
<add name="DatabaseException" type="FSoft.OVM.SystemFramework.OvmDatabaseException,FSoft.OVM.SystemFramework" postHandlingAction="NotifyRethrow">
<exceptionHandlers>
<add logCategory="toDB" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.XmlExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
priority="0" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging"
name="Logging Handler" />
</exceptionHandlers>
</add>
</exceptionPolicies>
</exceptionHandling>
Jun 1, 2007 at 4:07 AM
Edited Jun 1, 2007 at 4:10 AM
Obbs!!!!!! I 've found the problem. The reason is I am using CSLA framework and the code calls Exception policy is placed in a transaction of CSLA, so when the exception is thrown, rollback happens.

* Transactional(TransactionalTypes.TransactionScope)*
protected void DataPortal_Fetch(RootCriteria criteria)
{
try
{
// process business
}
catch (Exception ex)
{
bool rethrow = ExceptionPolicy.HandleException(ex, OVMExceptionPolicy.ServerGlobalPolicy);
if (rethrow)
{
throw;
}
}
}

I may have to find another way to place to logging code.

Tks all!