1

我正在尝试使用 Enterprise Library 5.0 为 WPF 应用程序设置日志记录,但出现以下错误:

System.Configuration.ConfigurationErrorsException was unhandled
  HResult=-2146232062
  Message=An error occurred creating the configuration section handler for loggingConfiguration: Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TextFormatterData' to type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormatterData'. (C:\Users\tomhu_000\Documents\GitHub\SourceLog\SourceLog.UI\bin\Debug\SourceLog.vshost.exe.Config line 16)
  Source=System.Configuration
  BareMessage=An error occurred creating the configuration section handler for loggingConfiguration: Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TextFormatterData' to type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormatterData'.
  Filename=C:\Users\tomhu_000\Documents\GitHub\SourceLog\SourceLog.UI\bin\Debug\SourceLog.vshost.exe.Config
  Line=16
  StackTrace:
       at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
       at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
       at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
       at System.Configuration.ConfigurationManager.GetSection(String sectionName)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource.DoGetSection(String sectionName)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileBasedConfigurationSource.GetSection(String sectionName)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ConfigSectionLocator.GetRegistrationsInternal(IConfigurationSource configurationSource, Func`3 registrationsAccessor)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ConfigSectionLocator.GetRegistrations(IConfigurationSource configurationSource)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.CompositeTypeRegistrationsProviderLocator.<GetRegistrations>b__0(ITypeRegistrationsProvider l, IConfigurationSource cs)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.CompositeTypeRegistrationsProviderLocator.<>c__DisplayClass5.<GetRegistrationsInternal>b__4(ITypeRegistrationsProvider l)
       at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.Unity.UnityContainerConfigurator.RegisterAllCore(IConfigurationSource configurationSource, ITypeRegistrationsProvider rootProvider)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ChangeTrackingContainerConfigurator.RegisterAll(IConfigurationSource configurationSource, ITypeRegistrationsProvider rootProvider)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.ConfigureContainer(ITypeRegistrationsProvider locator, IContainerConfigurator configurator, IConfigurationSource configSource)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.ConfigureContainer(IContainerConfigurator configurator, IConfigurationSource configSource)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.CreateDefaultContainer(IConfigurationSource configurationSource)
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.CreateDefaultContainer()
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.SetCurrentContainerIfNotSet()
       at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.get_Current()
       at Microsoft.Practices.EnterpriseLibrary.Logging.Logger.get_Writer()
       at Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(LogEntry log)
       at SourceLog.Model.LogSubscription.LogProvider_LogProviderException(Object sender, LogProviderExceptionEventArgs e) in C:\Users\tomhu_000\Documents\GitHub\SourceLog\SourceLog.Model\LogSubscription.cs:line 73
       at SourceLog.Plugin.TeamFoundationServer2010.TeamFoundationServer2010Plugin.CheckForNewLogEntries(Object state) in C:\Users\tomhu_000\Documents\GitHub\SourceLog\SourceLog.Plugin.TeamFoundationServer2010\TeamFoundationServer2010Plugin.cs:line 123
       at System.Threading.TimerQueueTimer.CallCallbackInContext(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.TimerQueueTimer.CallCallback()
       at System.Threading.TimerQueueTimer.Fire()
       at System.Threading.TimerQueue.FireNextTimers()
       at System.Threading.TimerQueue.AppDomainTimerCallback()
  InnerException: System.InvalidCastException
       HResult=-2147467262
       Message=Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TextFormatterData' to type 'Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormatterData'.
       Source=Microsoft.Practices.EnterpriseLibrary.Common
       StackTrace:
            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.ConfigurationSection.DeserializeSection(XmlReader reader)
            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)
       InnerException: 

这是我的配置:

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
        <add name="Event Log Listener" 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="" machineName="." traceOutputOptions="None" filter="Error" />
        <add name="Rolling Flat File Trace Listener" 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="app.log" formatter="Text Formatter" rollFileExistsBehavior="Increment"
         rollInterval="Midnight" maxArchivedFiles="14" traceOutputOptions="Callstack" />
    </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="Event Log Listener" />
            </listeners>
        </add>
    </categorySources>
    <specialSources>
        <allEvents switchValue="All" name="All Events">
            <listeners>
                <add name="Rolling Flat File Trace Listener" />
            </listeners>
        </allEvents>
        <notProcessed switchValue="All" name="Unprocessed Category" />
        <errors switchValue="All" name="Logging Errors &amp; Warnings">
            <listeners>
                <add name="Event Log Listener" />
            </listeners>
        </errors>
    </specialSources>
</loggingConfiguration>
4

1 回答 1

1

配置部分中的程序集引用没有引用我拥有的 DLL 的正确版本。更新为

Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0

它有效。

于 2012-10-14T20:20:33.133 回答