0

我在使用mvvm-light框架的Silverlight中使用Enterprise Library 5.0。 當我要去getInstace ExceptionManager類,如:無法獲取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> 
+0

你能發佈你的配置嗎?看起來異常處理塊正在使用日誌記錄,但無法找到日誌跟蹤偵聽器(在配置中)。 –

+0

好的,我添加了它.. – R76

回答

1

看起來您的配置正在引用名爲「Flat File Trace Listener」的跟蹤偵聽器,但沒有由該名稱定義的跟蹤偵聽器。

此外,您應該知道企業庫Silverlight日誌記錄應用程序塊不支持平面文件跟蹤偵聽器。有效的操作是:

  • 記錄到遠程WCF服務。
  • 記錄到獨立存儲。
  • 登錄到事件。
+0

但是當我要添加日誌塊時,它給了我同樣的錯誤。當我刪除了loggingBlock時,ExceptionHandling正常工作。但是當我想記錄異常時,我該怎麼辦? – R76