我正在尝试从企业库5.0升级到6.0,在过程中遇到了一些问题。也许这篇文章能够帮助其他人。
以下配置示例是用于将异常(静默式)记录到数据库中:
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" requirePermission="true" />
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" requirePermission="true" />
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database"
databaseInstanceName="LogContext" writeLogStoredProcName="WriteLog"
addCategoryStoredProcName="AddCategory" formatter="Text Formatter" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
template="Timestamp: {timestamp}{newline}..."
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="Exception">
<listeners>
<add name="Database Trace Listener" />
</listeners>
</add>
</categorySources>
</loggingConfiguration>
<exceptionHandling>
<exceptionPolicies>
<add name="UiPolicy">
<exceptionTypes>
<add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="None">
<exceptionHandlers>
<add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging"
logCategory="Exception" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
priority="0" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
我正在使用以下版本为5.0.414.0的程序集:
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging Microsoft.Practices.EnterpriseLibrary.Logging Microsoft.Practices.EnterpriseLibrary.Logging.Database
在 Enterprise Library 5.0 中,将所有这些用于 asp.net MVC 应用程序非常容易:
// ...
catch (Exception ex)
{
if (ExceptionPolicy.HandleException(ex, "UiPolicy")) throw;
// do something else
}
将程序集升级到Enterprise Library 6.0后,我收到了以下异常消息:
必须使用SetExceptionManager方法在ExceptionPolicy类中设置一个ExceptionManager。
那么,该怎么办呢?