将企业库5迁移到6

时间:2016-03-29 23:40:03

标签: c# asp.net-mvc logging exception-handling enterprise-library

试图从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后,出现以下异常消息:

  

必须使用SetExceptionManager方法在ExceptionPolicy类中设置ExceptionManager。

那么,该怎么办?

1 个答案:

答案 0 :(得分:5)

一步一步

要摆脱这种异常(归功于Randy's answer),我必须使用ExceptionManager,在我的情况下通过工厂。我在global.asax&#39;中使用它。 Application_Start()方法:

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

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

但是我的处理和记录设置只有一半。我现在在exceptionPolicyFactory.CreateManager()

处遇到以下异常
  

尚未为Logger静态类设置LogWriter。设置它调用Logger.SetLogWriter方法。

为了消除这一点,我添加了

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

现在说

  

数据库提供程序工厂未设置静态DatabaseFactory。设置一个提供者工厂,调用DatabaseFactory.SetProviderFactory方法或通过调用DatabaseFactory.SetDatabases方法指定自定义映射。

<{1>} 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");