Getting a Microsoft.Practices.ServiceLocation.ActivationException when logging exception to database.

Topics: Data Access Application Block, Exception Handling Application Block, Logging Application Block
Jul 19, 2013 at 1:59 AM
I have been using Entlib 5 (v5.0.505.0) to log exceptions to the Event log and this works well. However I have a need to now log exceptions to the Logging database (created with the script provided with the source code), and it is failing every time, giving me a Microsoft.Practices.ServiceLocation.ActivationException with the message:
Activation error occured while trying to get instance of type ExceptionPolicyImpl, key "Contractor Competencies Exception Policy"
This exception has an inner exception of type Microsoft.Practices.Unity.ResolutionFailedException with the message:
Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl", name = "Contractor Competencies Exception Policy".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.
This is turn has an inner exception of type System.InvalidOperationException with the message:
The type Database cannot be constructed. You must configure the container to supply this value.
This exception is thrown when I execute the code:
if ( ExceptionPolicy.HandleException(ex, Logger.ExceptionPolicy) )
{
 throw;
}
This is inside a catch block and Logger.ExceptionPolicy returns a string that exactly matches the configured value.

I have looked at many posts about this error and have implemented whatever recommendations I can find such as default database and so on but it has not fixed my issue. I have project references to EnterpriseLibrary.Common, Data, ExceptionHandling, ExceptionHandling.Logging, Logging, Logging.Database, ObjectBuilder2, ServiceLocation and Unity.

If I change back to the EventLog listener it works as expected.

This is my enitre config file:
<?xml version="1.0"?>
<configuration>
 <configSections>
  <section name="loggingConfiguration"
     type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     requirePermission="true" />
  <section name="exceptionHandling"
     type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     requirePermission="true" />
  <section name="dataConfiguration"
     type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     requirePermission="false" />
 </configSections>
 <loggingConfiguration name="Logging Application Block"
        tracingEnabled="true"
        defaultCategory="General"
        logWarningsWhenNoCategoriesMatch="true">
  <listeners>
   <add source="Contractor Competencies"
     formatter="Text Formatter"
     log="Application"
     machineName=""
     listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     traceOutputOptions="Callstack"
     filter="All"
     type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     name="Formatted EventLog TraceListener"/>
   <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     databaseInstanceName="LoggingDb"
     writeLogStoredProcName="WriteLog"
     addCategoryStoredProcName="AddCategory"
     formatter="Text Formatter" 
     name="Formatted Database TraceListener" />
  </listeners>
  <formatters>
   <add template="*Shortened for space reasons*"
     type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
     name="Text Formatter" />
  </formatters>
  <categorySources>
   <add switchValue="All"
     name="General">
    <listeners>
     <add name="Formatted Database TraceListener" />
    </listeners>
   </add>
  </categorySources>
  <specialSources>
   <allEvents switchValue="All"
        name="All Events"/>
   <notProcessed switchValue="All"
        name="Unprocessed Category"/>
   <errors switchValue="All"
     name="Logging Errors &amp; Warnings">
    <listeners>
     <add name="Formatted Database TraceListener"/>
    </listeners>
   </errors>
  </specialSources>
 </loggingConfiguration>
 <exceptionHandling>
  <exceptionPolicies>
   <add name="Contractor Competencies Exception Policy">
    <exceptionTypes>
     <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
       postHandlingAction="ThrowNewException"
       name="GeneralException">
      <exceptionHandlers>
       <add logCategory="General"
         eventId="100"
         severity="Error"
         title="Enterprise Library Exception Handling"
         formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         priority="0"
         useDefaultLogger="false"
         type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         name="Logging Handler"/>
       <add exceptionMessage="An error has occurred. If the problem persists, please contact IS Support on extension 6654."
         exceptionMessageResourceName=""
         exceptionMessageResourceType=""
         replaceExceptionType="System.ApplicationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
         type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         name="Replace Handler"/>
      </exceptionHandlers>
     </add>
    </exceptionTypes>
   </add>
  </exceptionPolicies>
 </exceptionHandling>
 <dataConfiguration defaultDatabase="LoggingDb" />
 <connectionStrings>
  <add name="LoggingDb"
    connectionString="Password=******;User ID=LoggingUser;Initial Catalog=Logging;Data Source=TSTDVP01\DevSQL"
    providerName="System.Data.SqlClient" />
 </connectionStrings>
</configuration>
Jul 19, 2013 at 7:13 AM
The message usually points to the configuration being invalid but the configuration looks OK -- it works fine in a test app. So there must be some other issue with the setup or the environment.

It might be easier if you could upload a small sample application that demonstrates the problem.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to