Logging errors when migrating from 4.1 to 5.0

Topics: Logging Application Block
Dec 21, 2010 at 11:35 PM

I recently migrated my application from using 4.1 version of the Enterprise Library to 5.0. Now I am getting the exception/error:

System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for loggingConfiguration: Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CategoryFilterData' to type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LogFilterData'. (D:\Program Files (x86)\BsiServices\ServiceHost\Bin\Buyseasons.WebServices.BsiServices.Host.exe.Config line 97) ---> System.InvalidCastException: Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CategoryFilterData' to type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LogFilterData'.
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.PolymorphicConfigurationElementCollection`1.OnDeserializeUnrecognizedElement(String elementName, XmlReader reader)
   at System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
   at System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
   at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line)

Any idea on how to prevent this exception? This is pretty deep of a call stack and at the root I am just calling Logger.Write. I am not sure how to prevent this invalid cast exception.

Thank you for your ideas.

Kevin

Dec 22, 2010 at 12:05 AM

 Did you update your configuration file?  If yes, how?

 You can send the config file to me or post it here so I could try it on my end. 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 22, 2010 at 5:22 AM

I used the Enterprise library configuration file application to change/update the configuration file. Apparently that didn't get applied because now I am getting the error/exception:

System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for loggingConfiguration: Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData' to type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TraceListenerData'. (D:\Program Files (x86)\BsiServices\ServiceHost\Bin\Buyseasons.WebServices.BsiServices.Host.exe.Config line 30) ---> System.InvalidCastException: Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData' to type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TraceListenerData'.
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.PolymorphicConfigurationElementCollection`1.OnDeserializeUnrecognizedElement(String elementName, XmlReader reader)
   at System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
   at System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
   at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line)

Different error but the same effect.

The configuration section looks like:

	<configSections>
		<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null" requirePermission="true" />
  <sectionGroup name="CommerceServer">
			<section name="application" type="Microsoft.CommerceServer.Runtime.Configuration.CommerceApplicationSectionHandler, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<section name="authentication" type="Microsoft.CommerceServer.Runtime.Configuration.CommerceAuthenticationSectionHandler, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<section name="pipelines" type="Microsoft.CommerceServer.Runtime.Configuration.CommercePipelineSectionHandler, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<section name="caches" type="Microsoft.CommerceServer.Runtime.Configuration.CommerceCacheSectionHandler, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<section name="messageManager" type="Microsoft.CommerceServer.Runtime.Configuration.CommerceMessageManagerSectionHandler, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<section name="catalog" type="Microsoft.CommerceServer.Runtime.Configuration.CommerceCatalogSectionHandler, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<section name="orders" type="Microsoft.CommerceServer.Runtime.Configuration.CommerceOrdersSectionHandler, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<section name="profiles" type="Microsoft.CommerceServer.Runtime.Configuration.CommerceProfilesSectionHandler, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<section name="contentSelection" type="Microsoft.CommerceServer.Runtime.Configuration.CommerceContentSelectionSectionHandler, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<section name="commerceEvent" type="Microsoft.CommerceServer.Runtime.Configuration.EventLoggerConfigurationHandler, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<section name="expressions" type="Microsoft.CommerceServer.Runtime.Configuration.CommerceExpressionSectionHandler, Microsoft.CommerceServer.Runtime, Version=6.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
		</sectionGroup>
		<section name="commerceComponents" type="CommerceComponents.Configuration.ComponentConfiguration, CommerceComponents"/>
		<section name="commerceSite" type="CommerceSite.Configuration.SiteConfiguration"/>
  <section name="BsiConfigSection" type="BuySeasons.WebServices.BsiServices.Configuration.BsiServicesConfigurationSection, BuySeasons.WebServices.BsiServices.Configuration" />
	</configSections>
 <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
  <listeners>
   <add name="Event Log Destination" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
    source="Bsi Web Services" formatter="Text Formatter" machineName="."
    traceOutputOptions="LogicalOperationStack" />
   <add name="Flat File Destination" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
    fileName="trace.log" header="----------------header------------------------"
    footer="----------------footer------------------------" formatter="Text Formatter" />
  </listeners>
  <formatters>
   <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
    template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
    name="Text Formatter" />
  </formatters>
  <logFilters>
   <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
    categoryFilterMode="AllowAllExceptDenied" name="Category">
    <categoryFilters>
     <add name="Troubleshooting" />
    </categoryFilters>
   </add>
   <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
    enabled="true" name="LogEnabled Filter" />
  </logFilters>
  <categorySources>
   <add switchValue="All" name="Data Access Events">
    <listeners>
     <add name="Flat File Destination" />
    </listeners>
   </add>
   <add switchValue="All" name="Debug">
    <listeners>
     <add name="Flat File Destination" />
    </listeners>
   </add>
   <add switchValue="All" name="General">
    <listeners>
     <add name="Event Log Destination" />
    </listeners>
   </add>
   <add switchValue="All" name="Troubleshooting">
    <listeners>
     <add name="Event Log Destination" />
    </listeners>
   </add>
   <add switchValue="All" name="Trace">
    <listeners>
     <add name="Flat File Destination" />
    </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="Event Log Destination" />
    </listeners>
   </errors>
  </specialSources>
 </loggingConfiguration>

 

Dec 22, 2010 at 5:59 AM

Your config shows that you used the configuration tool built from the unsigned EntLib assemblies, the ones from the source code folder.  If you are referencing the assemblies from the EntLib installation folder (C:\Program Files\Microsoft Enterprise Library 5.0\bin), which are the strongly-named versions, then you should use the configuration tool (EntLibConfig.exe) from that folder as well which should be really integrated in Visual Studio 2008. 

You can verify that you used the correct config tool thru the PublicKeyToken attribute of entlib assemblies.  If you open your config in xml editor, the PublicKeyToken attribute of entlib assemblies should have a value instead of null.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 22, 2010 at 2:26 PM

Now I am getting the exception "System.IO.FileLoadException: A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)"

I copied the assemblies from the binary distribution. The configuration looks like:

<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" />
	</configSections>
 <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
  <listeners>
   <add name="Event Log Destination" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
    source="Bsi Web Services" formatter="Text Formatter" machineName="."
    traceOutputOptions="LogicalOperationStack" />
   <add name="Flat File Destination" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
    fileName="trace.log" header="----------------header------------------------"
    footer="----------------footer------------------------" formatter="Text Formatter" />
  </listeners>
  <formatters>
   <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
    template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
    name="Text Formatter" />
  </formatters>
  <logFilters>
   <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
    categoryFilterMode="AllowAllExceptDenied" name="Category">
    <categoryFilters>
     <add name="Troubleshooting" />
    </categoryFilters>
   </add>
   <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
    enabled="true" name="LogEnabled Filter" />
  </logFilters>
  <categorySources>
   <add switchValue="All" name="Data Access Events">
    <listeners>
     <add name="Flat File Destination" />
    </listeners>
   </add>
   <add switchValue="All" name="Debug">
    <listeners>
     <add name="Flat File Destination" />
    </listeners>
   </add>
   <add switchValue="All" name="General">
    <listeners>
     <add name="Event Log Destination" />
    </listeners>
   </add>
   <add switchValue="All" name="Troubleshooting">
    <listeners>
     <add name="Event Log Destination" />
    </listeners>
   </add>
   <add switchValue="All" name="Trace">
    <listeners>
     <add name="Flat File Destination" />
    </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="Event Log Destination" />
    </listeners>
   </errors>
  </specialSources>
 </loggingConfiguration>

 

Dec 22, 2010 at 3:53 PM

If I don't have signed assemblies then I can use the source version of the configuration editor. Right?

I still have the original problem or are they related?

Thank you.

Kevin

Dec 23, 2010 at 5:35 AM
Edited Dec 23, 2010 at 5:36 AM

Yes you can still use the source code version of assemblies, just make sure the Assembly Set in your solution is correctly pointed to the source code.

With this, can you also check if the Assembly Set of your solution is properly pointing to the correct assemblies where you reference your project. Also, it'll be better if you can provide us a sample repro project for this for us to further investigate since I cannot reproduce your problem by just copy and pasting your configuration.

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