0

我正在使用 mvvm-light 框架在 Silverlight 中使用 Enterprise Library 5.0。当我要获取 ExceptionManager 类的实例时,例如:

 var exceptionManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
  exceptionManager.HandleException(E.Error, "Policy");

我收到这种类型的错误消息

Activation error occured while trying to get instance of type ExceptionManager, key ""

Resolution of the dependency failed, type = Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager", name = "(none)"
  Exception occurred while: while resolving.Exception is: InvalidOperationException - The type TraceListener cannot be constructed. You must configure the container to supply this value
 -----------------------------------------------
 At the time of the exception, the container was:

 Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManagerImpl,ExceptionManager.__default__ (mapped from Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager, (none))
Resolving parameter "exceptionPolicies" of constructor Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManagerImpl(System.Collections.Generic.IEnumerable`1[[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Silverlight, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] exceptionPolicies, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Instrumentation.IDefaultExceptionHandlingInstrumentationProvider instrumentationProvider)
Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl,Policy
Resolving parameter "policyEntries" of constructor Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl(System.String policyName, System.Collections.Generic.IEnumerable`1[[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Silverlight, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] policyEntries)
  Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry,Policy.All Exceptions
  Resolving parameter "handlers" of constructor Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry(System.Type exceptionType, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PostHandlingAction postHandlingAction, System.Collections.Generic.IEnumerable`1[[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.IExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Silverlight, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] handlers, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Instrumentation.IExceptionHandlingInstrumentationProvider instrumentationProvider)
    Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler,Policy.All Exceptions.Logging Exception Handler (mapped from Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.IExceptionHandler, Policy.All Exceptions.Logging Exception Handler)
    Resolving parameter "writer" of constructor Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler(System.String logCategory, System.Int32 eventId, Microsoft.Practices.EnterpriseLibrary.Logging.Diagnostics.TraceEventType severity, System.String title, System.Int32 priority, System.Type formatterType, Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter writer)
      Resolving Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterImpl,LogWriter.__default__ (mapped from Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, (none))
      Resolving parameter "structureHolder" of constructor Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterImpl(Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolder structureHolder, Microsoft.Practices.EnterpriseLibrary.Logging.Instrumentation.ILoggingInstrumentationProvider instrumentationProvider, Microsoft.Practices.EnterpriseLibrary.Logging.IAsyncTracingErrorReporter asyncTracingErrorReporter)
        Resolving Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolder,LogWriterStructureHolder.__default__ (mapped from Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolder, (none))
        Resolving parameter "traceSources" of constructor Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolder(System.Collections.Generic.IEnumerable`1[[Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter, Microsoft.Practices.EnterpriseLibrary.Logging.Silverlight, Version=5.0.505.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] filters, System.Collections.Generic.IEnumerable`1[[System.String, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] traceSourceNames, System.Collections.Generic.IEnumerable`1[[Microsoft.Practices.EnterpriseLibrary.Logging.LogSource, Microsoft.Practices.EnterpriseLibrary.Logging.Silverlight, Version=5.0.505.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] traceSources, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource allEventsTraceSource, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource notProcessedTraceSource, Microsoft.Practices.EnterpriseLibrary.Logging.LogSource errorsTraceSource, System.String defaultCategory, System.Boolean tracingEnabled, System.Boolean logWarningsWhenNoCategoriesMatch, System.Boolean revertImpersonation)
          Resolving Microsoft.Practices.EnterpriseLibrary.Logging.LogSource,General
          Resolving parameter "traceListeners" of constructor Microsoft.Practices.EnterpriseLibrary.Logging.LogSource(System.String name, System.Collections.Generic.IEnumerable`1[[Microsoft.Practices.EnterpriseLibrary.Logging.Diagnostics.TraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Silverlight, Version=5.0.505.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] traceListeners, Microsoft.Practices.EnterpriseLibrary.Logging.Diagnostics.SourceLevels level, System.Boolean autoFlush, Microsoft.Practices.EnterpriseLibrary.Logging.Instrumentation.ILoggingInstrumentationProvider instrumentationProvider)
            Resolving Microsoft.Practices.EnterpriseLibrary.Logging.Diagnostics.TraceListener,Flat File Trace Listener

在线

 var exceptionManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();

我的配置文件是:

   <?xml version="1.0" encoding="utf-8"?>
    <ResourceDictionary           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:el="http://schemas.microsoft.com/practices/2011/entlib">
     <el:ExceptionHandlingSettings x:Key="exceptionHandling">
     <el:ExceptionHandlingSettings.ExceptionPolicies>
     <el:ExceptionPolicyData Name="LogPolicy">
     <el:ExceptionPolicyData.ExceptionTypes>
      <el:ExceptionTypeData Name="All Exceptions" TypeName="System.Exception, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e">
        <el:ExceptionTypeData.ExceptionHandlers>
          <el:LoggingExceptionHandlerData LogCategory="General" FormatterTypeName="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Silverlight, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Priority="-1" Name="Logging Exception Handler" />
        </el:ExceptionTypeData.ExceptionHandlers>
      </el:ExceptionTypeData>
    </el:ExceptionPolicyData.ExceptionTypes>
  </el:ExceptionPolicyData>
 </el:ExceptionHandlingSettings.ExceptionPolicies>
 </el:ExceptionHandlingSettings>
 <el:LoggingSettings DefaultCategory="General" x:Key="loggingConfiguration">
  <el:LoggingSettings.TraceSources>
  <el:TraceSourceData Name="General">
    <el:TraceSourceData.TraceListeners>
      <el:TraceListenerReferenceData Name="Flat File Trace Listener" />
    </el:TraceSourceData.TraceListeners>
  </el:TraceSourceData>
 </el:LoggingSettings.TraceSources>
 <el:LoggingSettings.SpecialTraceSources>
  <el:SpecialTraceSourcesData>
    <el:SpecialTraceSourcesData.AllEventsTraceSource>
      <el:TraceSourceData Name="All Events" />
    </el:SpecialTraceSourcesData.AllEventsTraceSource>
    <el:SpecialTraceSourcesData.NotProcessedTraceSource>
      <el:TraceSourceData Name="Unprocessed Category" />
    </el:SpecialTraceSourcesData.NotProcessedTraceSource>
    <el:SpecialTraceSourcesData.ErrorsTraceSource>
      <el:TraceSourceData Name="Logging Errors &amp; Warnings">
        <el:TraceSourceData.TraceListeners>
          <el:TraceListenerReferenceData Name="Flat File Trace Listener" />
        </el:TraceSourceData.TraceListeners>
      </el:TraceSourceData>
    </el:SpecialTraceSourcesData.ErrorsTraceSource>
  </el:SpecialTraceSourcesData>
  </el:LoggingSettings.SpecialTraceSources>
  </el:LoggingSettings>
  </ResourceDictionary>
4

1 回答 1

1

看起来您的配置引用了一个名为“平面文件跟踪侦听器”的跟踪侦听器,但该名称没有定义跟踪侦听器。

此外,您应该知道企业库 Silverlight 日志记录应用程序块不支持平面文件跟踪侦听器。有效的行动是:

  • 记录到远程 WCF 服务。
  • 记录到隔离存储。
  • 记录到事件。
于 2012-05-02T05:11:06.103 回答