How to cover all exception types in policy

Topics: Exception Handling Application Block, Logging Application Block
Mar 3, 2011 at 8:43 AM

I have a couple of exception policies, and each exception is logged and passed through using specific exception handlers:

<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="Errors" logWarningsWhenNoCategoriesMatch="true">
    
    <listeners>
      <add name="Error Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="error.log" header="------------------Start Log------------------"
        footer="-------------------End Log-------------------" formatter="Text Formatter"
        traceOutputOptions="None" filter="All" />
    </listeners>
    
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="Timestamp: {timestamp}&#xD;&#xA;&#xD;&#xA;           Message: {message}&#xD;&#xA;&#xD;&#xA;           Category: {category}&#xD;&#xA;&#xD;&#xA;           Priority: {priority}&#xD;&#xA;&#xD;&#xA;           EventId: {eventid}&#xD;&#xA;&#xD;&#xA;           Severity: {severity}&#xD;&#xA;&#xD;&#xA;           Title: {title}&#xD;&#xA;&#xD;&#xA;           Machine: {machine}&#xD;&#xA;&#xD;&#xA;           Application Domain: {appDomain}&#xD;&#xA;&#xD;&#xA;           Process Id: {processId}&#xD;&#xA;&#xD;&#xA;           Process Name: {processName}&#xD;&#xA;&#xD;&#xA;           Win32 Thread Id: {win32ThreadId}&#xD;&#xA;&#xD;&#xA;           Thread Name: {threadName}&#xD;&#xA;&#xD;&#xA;           Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        name="Text Formatter" />
    </formatters>
    
    <categorySources>
      <add switchValue="Error" name="Errors">
        <listeners>
          <add name="Error Log Listener" />
        </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="Error Log Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  
  <exceptionHandling>
    <exceptionPolicies>
      
      <add name="BusinessLogicLayerPolicy">
        <exceptionTypes>
          <add name="DomainInternalMessageException" type="ALT_Shared.Exceptions.DomainInternalMessageException, ALT_Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Internal Message Exception" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="Errors" eventId="200" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="true" />
              <add type="ALT_Shared.ExceptionHandlers.DomainInternalMessageExceptionHandler, ALT_Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Internal Message Exception Handler" />
            </exceptionHandlers>
          </add>
          <add name="EntityValidationException" type="ALT_Shared.Exceptions.EntityValidationException, ALT_Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Log Entity Validation Exception" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="Errors" eventId="200" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="true" />
              <add type="ALT_Shared.ExceptionHandlers.EntityValidationExceptionHandler, ALT_Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Entity Validation Exception Handler" />
            </exceptionHandlers>
          </add>
          <add name="Exception" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Log System Exception" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="Errors" eventId="200" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="true" />
              <add type="ALT_Shared.ExceptionHandlers.DomainExceptionHandler, ALT_Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="System Exception Handler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      
      <add name="DataAccessLayerPolicy">
        <exceptionTypes>
          <add name="DomainInternalMessageException" type="ALT_Shared.Exceptions.DomainInternalMessageException, ALT_Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Internal Message Exception" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="Errors" eventId="200" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="true" />
              <add type="ALT_Shared.ExceptionHandlers.DomainInternalMessageExceptionHandler, ALT_Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Internal Message Exception Handler" />
            </exceptionHandlers>
          </add>
          <add name="EntityValidationException" type="ALT_Shared.Exceptions.EntityValidationException, ALT_Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Entity Validation Exception" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="Errors" eventId="200" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="true" />
              <add type="ALT_Shared.ExceptionHandlers.EntityValidationExceptionHandler, ALT_Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Entity Validation Exception Handler" />
            </exceptionHandlers>
          </add>
          <add name="OptimisticConcurrencyException" type="System.Data.OptimisticConcurrencyException, System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Log Optimistic Concurrency Exception" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="Errors" eventId="200" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="true" />
              <add type="ALT_Shared.ExceptionHandlers.OptimisticConcurrencyExceptionHandler, ALT_Shared, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Optimistic Concurrency Exception Handler" />
            </exceptionHandlers>
          </add>
          <add name="SqlDatabaseException" type="System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Log Sql Exception" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="Errors" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="true" />
              <add type="ALT_Shared.ExceptionHandlers.DomainExceptionHandler, ALT_Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Sql Exception Handler" />
            </exceptionHandlers>
          </add>
          <add name="Exception" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Log System Exception" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="Errors" eventId="200" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="true" />
              <add type="ALT_Shared.ExceptionHandlers.DomainExceptionHandler, ALT_Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="System Exception Handler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      
    </exceptionPolicies>
  </exceptionHandling>

How do I manage exception types not covered in the policies above? Is there a catch-all handler that will capture any other error types? The reason  I ask is that I want to be sure that all exceptions are logged, and not excluded from the log file.

I added a new exception type to cover mscorlib, System, TimeoutException, and assigned a Logging Exception Handler to ensure it gets logged. Is it just a case of adding these one by one, or is there a way to catch-all?

Thanks

Martin

Mar 3, 2011 at 9:29 AM

Hi Martin,

In EHAB by default if you create a new policy the configured Exception Type is the System.Exception which catches all exceptions. HTH.

Exception Policy Default Setting

 

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Mar 3, 2011 at 9:34 AM

OK, thanks