Cannot Create LogEntry with ExceptionHandler

Topics: Exception Handling Application Block
Mar 7, 2012 at 3:32 AM

I am using an exception policy that logs and replaces an exception as follows:

 

    public static class ExceptionHandler
    {
        static ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();

        public static void ManageException(Exception ex, string policyName)
        {
            exManager.HandleException(ex, policyName);
        }
    }

exception policy config:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <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>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Formatted EventLog TraceListener" 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="Enterprise Library Logging" formatter="Text Formatter"
        log="Application" machineName="" traceOutputOptions="None" />
      <add name="Flat File Trace 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="C:\Projects\Pricing Strategy\libAndHost\Log\TestExceptions.log"
        formatter="Text Formatter" />
    </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}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
          <add name="Flat File Trace 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="Formatted EventLog TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="RootExceptionPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="There was an error! (CT)" faultContractType="eTest.Myfault, TestExceptionLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Fault Contract Exception Handler">
                <mappings>
                  <add source="Message" name="Message" />
                  <add source="StackTrace" name="StackTrace" />
                </mappings>
              </add>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      <add name="LoggingAndReplacingException">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Logging Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="General" eventId="9000" severity="Error" title="General Exception"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                priority="5" />
              <add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="An application error occurred and has been logged. Please contact your administrator."
                replaceExceptionType="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      <add name="Policy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                replaceExceptionType="Manulife.ExceptionLib.ManulifeBusinessException, ExceptionLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IReportingService" />
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="eTest.ReportingService">
        <endpoint address="http://localhost/ReportingService/ReportingService.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IReportingService"
          contract="eTest.IReportingService" />
        <endpoint address="http://localhost:3585/ExceptionTests/ReportingService.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IReportingService"
          name="LocalAspDevServer" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 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="false"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

 

I tried hooking up the entlib 5.0 source code to the entlib 5.0 .dll's but ran into the following issues:

- tried creating a separate project with the entlib 5.0 source code and then reference the project as supposed to the .dll, results in the following:

An error occurred creating the configuration section handler for exceptionHandling: Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) (C:\Projects\Pricing Strategy\libAndHost\ExceptionTests\web.config line 5)

 

Also tried researching the following on google but to no avail - again the point is just to attach the entlib source code so I can see why the logger doesn't log:

{"Resolution of the dependency failed, type = \"Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager\", name = \"(none)\".\r\nException

occurred while: while resolving.\r\nException is: InvalidOperationException - The type ExceptionManager cannot be constructed. You must configure the

container to supply this value.\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n  Resolving

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager,(none)\r\n"}

Thanks and any help is appreciated.

 

 

Mar 7, 2012 at 5:50 AM

Hi,

To get debug working the trick is to change the configuration file.  Replace "PublicKeyToken=31bf3856ad364e35" with "PublicKeyToken=null" for all enterprise library assemblies.  This is required because you are no longer using the signed version of Enterprise Library (but a local debug build). 

Since you are running in web environment you should double check that the "Enterprise Library Logging" source has been created.  It will require Administrator privilege to create the appropriate registry keys which ASP.NET/Cassini (usually) does not have.

Also, as a general trouble shooting approach I would try to remove the event log trace listener (since this can often have permission problems) and just go with a flat file trace listener that writes to a location that will not have any permission issues.  If that doesn't work then the problem is probably with the logging so I would just try and test logging without exception handling to remove that potential complication.  If it did work (wrote to flat file) then I would add back the event log and troubleshoot that (or try to log to the event log without exception handling).

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