将企业库 5 迁移到 6

Migrate Enterprise Library 5 to 6

试图从 Enterprise Library 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 后,我收到以下异常消息:

Must set an ExceptionManager in the ExceptionPolicy class using the SetExceptionManager method.

那么,怎么办?

循序渐进

要消除此异常(归功于 Randy's answer),必须使用 ExceptionManager,在我的例子中是通过工厂。我在 global.asax' Application_Start() 方法中使用它:

protected void Application_Start()
{
    IConfigurationSource source = ConfigurationSourceFactory.Create();

    var exceptionPolicyFactory = new ExceptionPolicyFactory(source);
    var exceptionManager = exceptionPolicyFactory.CreateManager();
    ExceptionPolicy.SetExceptionManager(exceptionManager);
}

但是根据我的处理和日志记录设置,这只是完成了一半。我现在在 exceptionPolicyFactory.CreateManager()

处得到以下异常

The LogWriter has not been set for the Logger static class. Set it invoking the Logger.SetLogWriter method.

为了让这个消失,我添加了

var logwriterFactory = new LogWriterFactory(source);
var logWriter = logwriterFactory.Create();
Logger.SetLogWriter(logWriter);

现在说

Database provider factory not set for the static DatabaseFactory. Set a provider factory invoking the DatabaseFactory.SetProviderFactory method or by specifying custom mappings by calling the DatabaseFactory.SetDatabases method.

logwriterFactory.Create()。所以还有一件事要补充:

DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(source));

完整的解决方案

加起来我的 Application_Start() 现在是

protected void Application_Start()
{
    IConfigurationSource source = ConfigurationSourceFactory.Create();
    DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(source));

    var logwriterFactory = new LogWriterFactory(source);
    var logWriter = logwriterFactory.Create();
    Logger.SetLogWriter(logWriter);

    var exceptionPolicyFactory = new ExceptionPolicyFactory(source);
    var exceptionManager = exceptionPolicyFactory.CreateManager();
    ExceptionPolicy.SetExceptionManager(exceptionManager);
}

而配置和实际的异常处理代码保持不变:

ExceptionPolicy.HandleException(exceptionToHandle, "UiPolicy");