Unable to handle Logging Exception handler

Topics: Building and extending application blocks, Exception Handling Application Block
Dec 27, 2011 at 11:30 AM

Hi

In my project I have used Enterprise Library 5.0 to log exceptions with a custom exception formatter.

In release mode if the bin folder doesn't contain .PDB files then I am getting the exception :

ExceptionHandlingException

Unable to handle exception: 'LoggingExceptionHandler'

any suggestions, anybody?

Dec 28, 2011 at 4:20 AM

That sounds very odd.  To clarify: it works in Debug mode and it works in Release mode with PDB files present but if you remove the PDB files (without a rebuild) then an exception is thrown?

Can you find out what the full exception and stack trace (including inner exceptions) is?

You could also try to debug the application with "break when an exception is thrown" set to see if any exceptions are being thrown and swallowed.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Dec 28, 2011 at 5:27 AM
Edited Dec 28, 2011 at 5:48 AM

The scenerio is that I have one method called 'PublishError' which calls Eterprise Library's HandleException method and the exception is logged in ExceptionLog flie.

If there is any error in PublishError method the WriteException method is called to write that exception using HandleException in another file DiagnosticLog but without PDB files the following exception is getting stored in ExceptionLog

The following is the detailed exception without pdb files:

Unable to handle exception: 'LoggingExceptionHandler'.
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry.ExecuteHandlerChain(Exception ex, Guid handlingInstanceID)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry.Handle(Exception exceptionToHandle)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl.HandleException(Exception exceptionToHandle)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception exceptionToHandle, String policyName)
   at Framework.Diagnostics.Exceptions.ExceptionLogger.WriteException(Exception oException)
   at Framework.Diagnostics.Exceptions.ExceptionLogger.PublishError(CustomException oException)
   at Framework.DAO.SqlHelper.ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, String commandText, SqlParameter[] commandParameters) //here I have a connection string null exception
   at Framework.DAO.SqlHelper.ExecuteReader(SqlConnection connection, CommandType commandType, String commandText, SqlParameter[] commandParameters)
   at Framework.DAO.SqlHelper.ExecuteReader(SqlConnection connection, CommandType commandType, String commandText)
   at Common.CommonDAO.EntitlementDAO.Read(Query Objquery)

 If I store the PDB of Framework.DAO in Bin then error is logged properly.

Dec 28, 2011 at 6:33 AM

Can you try enabling logging in the blocks by adding an instrumentation section:

  <section name="instrumentationConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration.InstrumentationConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
...
  <instrumentationConfiguration eventLoggingEnabled="true" />

With that enabled you should see an entry in the EventLog Application log under the "Enterprise Library ExceptionHandling" source.  

Can you post the code for PublishError and WriteException along with the configuration?

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Dec 28, 2011 at 9:13 AM
Edited Dec 28, 2011 at 10:10 AM

My code and configuration

----Publish Error----
public static bool PublishError(CustomException oException) {
// Initialises status value to false
bool _status = false;
try
{
// Checking for null object
if (oException != null)
{
if (oException.InnerException != null)
oException.StackTrace = oException.InnerException.StackTrace;
_status = ExceptionPolicy.HandleException(oException, "ExceptionLogging");
}
}
catch (Exception oEx)
{
WriteException(oEx);
}
return _status;
}

----Write Exception----
private static void WriteException(Exception oException)
{
if (oException != null)
{
// Writes exception details into log file.
try
{
ExceptionPolicy.HandleException(oException, "Diagnostic");
}
catch (Exception)
{
throw;
}
}
}

----Configuration----

<?xml version="1.0"?>
<configuration>

  <configSections>
        <section name="cachingConfiguration"  type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="loggingConfiguration"  type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="exceptionHandling"     type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
    
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

  <cachingConfiguration defaultCacheManager="Cache Manager">
    <cacheManagers>
      <add name="Cache Manager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        expirationPollFrequencyInSeconds="1350" maximumElementsInCacheBeforeScavenging="1000"
        numberToRemoveWhenScavenging="60" backingStoreName="NullBackingStore" />
    </cacheManagers>
    <backingStores>
      <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="NullBackingStore" />
    </backingStores>
  </cachingConfiguration>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="ExceptionHandling">
    <listeners>
      <add name="Audit Trace Event Log" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        source="Logging" formatter="CustomFileFormatter" log="Application"
        machineName="." traceOutputOptions="None" />
      <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        type="Framework.Logging.ExtendedDatabaseTraceListener, Framework.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        name="Database Log" />
      <add name="Audit Rolling Log" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="Logs\AuditLog.txt" footer="^^--------------------------------------------------^^"
        formatter="CustomFileFormatter" header="" rollInterval="Day"
        maxArchivedFiles="1" />
      <add name="Exception Rolling Log" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="Logs\ExceptionLog.txt" footer="^^----------------------------------------------------------^^"
        formatter="Text Formatter" header="" rollSizeKB="1024" maxArchivedFiles="1" />
      <add name="Trace Rolling Log" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="Logs\TraceLog.txt" footer="^^-----------------------------------------------^^"
        formatter="CustomFileFormatter" header="" rollInterval="Day"
        maxArchivedFiles="1" />
      <add name="Exception Event Log" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        source="ExceptionLog" formatter="Text Formatter" log="Application" />
      <add name="Diagnostic Log" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="Logs\DiagnosticLog.txt" footer="^^------------------------------------------------^^"
        formatter="Text Formatter" header="" rollInterval="Day" maxArchivedFiles="1" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="{message}" name="Text Formatter" />
      <add type="Framework.Logging.CustomFileFormatter, Framework.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        name="CustomFileFormatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="ExceptionHandling">
        <listeners>
          <add name="Exception Rolling Log" />
          <add name="Database Log" />
        </listeners>
      </add>
      <add switchValue="All" name="AuditLogging">
        <listeners>
          <add name="Database Log" />
        </listeners>
      </add>
      <add switchValue="All" name="TraceLogging">
        <listeners>
          <add name="Database Log" />
        </listeners>
      </add>
      <add switchValue="All" name="Diagnostics">
        <listeners>
          <add name="Diagnostic Log" />
        </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="Audit Trace Event Log" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="ExceptionLogging">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="ExceptionHandling" eventId="100" severity="Error"
                title="Enterprise Library Exception Handling" formatterType="Framework.Diagnostics.Exceptions.DMPCustomExceptionFormatter, Framework.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                priority="0" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      <add name="Diagnostic">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="Diagnostics" eventId="101" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Framework.Diagnostics.Exceptions.DMPCustomExceptionFormatter, Framework.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                priority="0" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
</configuration>

Dec 29, 2011 at 2:18 AM

I wasn't able to reproduce the issue.  Is it possible to create a complete project that reproduces the issue that I could look at?  There is quite a lot going on so it would be good to narrow the issue down.

As a general approach I would start removing functionality one by one and see if/when the issue disappears.  That could hopefully nail it down.

I'm curious what the CustomException class does -- it looks like it shadows StackTrace by adding a setter.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Dec 29, 2011 at 6:42 AM

The scenerio is that I have one method called 'PublishError' which calls Eterprise Library's HandleException method and the exception is logged in ExceptionLog flie.

If there is any error in PublishError method the WriteException method is called to write that exception using HandleException in another file DiagnosticLog but without PDB files the following exception is getting stored in ExceptionLog

The following is the detailed exception without pdb files:

Unable to handle exception: 'LoggingExceptionHandler'.
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry.ExecuteHandlerChain(Exception ex, Guid handlingInstanceID)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry.Handle(Exception exceptionToHandle)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl.HandleException(Exception exceptionToHandle)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception exceptionToHandle, String policyName)
   at Framework.Diagnostics.Exceptions.ExceptionLogger.WriteException(Exception oException)
   at Framework.Diagnostics.Exceptions.ExceptionLogger.PublishError(CustomException oException)
   at Framework.DAO.SqlHelper.ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, String commandText, SqlParameter[] commandParameters) //here I have a connection string null exception
   at Framework.DAO.SqlHelper.ExecuteReader(SqlConnection connection, CommandType commandType, String commandText, SqlParameter[] commandParameters)
   at Framework.DAO.SqlHelper.ExecuteReader(SqlConnection connection, CommandType commandType, String commandText)
   at Common.CommonDAO.EntitlementDAO.Read(Query Objquery)

 If I store the PDB of Framework.DAO in Bin then error is logged properly.

Expanding on the issue - In a webApp I am invoking a WCF service which I have published on local IIS 7, this service references some others projects(like Framework.DAO, Framework.Diagnostics etc.).

In the Bin folder of the published service I have removed the PDB files, and the exception is rising in refrenced code.

Also, the CustomException class extends ApplicationException and has some properties for storing exception details and one string property named StackTrace

Dec 29, 2011 at 2:01 PM

problem solved...in Framework.DAO somewhere I was throwing the CustomException object to higher layer.