Upgrade to Enterprise Library 6.0 giving issues with 'EnterpriseLibraryContainer'

故事扮演 提交于 2019-11-30 22:59:37

The typical approach would be to bootstrap the block, register the appropriate objects with Unity and have Unity inject the dependencies.

For example, if you are using logging then you would bootstrap the block:

LogWriterFactory logWriterFactory = new LogWriterFactory(); 
LogWriter logWriter = logWriterFactory.Create();

and register the LogWriter with the UnityContainer:

IUnityContainer container = new UnityContainer();
// Register LogWriter as singleton
container.RegisterInstance<LogWriter>(logWriter);

If you were using the EnterpriseLibraryContainer as a service locator and wish to keep using that same approach then you could create/wrap a service locator implementation or create a static helper method. Unity comes with UnityServiceLocator which you could reuse.

If you aren't using Unity, another approach would be to bootstrap the blocks and then replace the calls to EnterpriseLibraryContainer.Current.GetInstance<>() with the static facade methods (e.g. Logger.Write()).

I've really struggled with this so here is my working upgrade from Enterprise 5 to v6 (fluent). I mention that in the Codeplex Migration PDF it still shows EnterpriseLibraryContainer, which is no longer valid.

// app.config

<configSections>
       <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>

// my logger class using System; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.Logging;

/// <summary>
/// Set up the Enterprise logging class
/// </summary>
public static class Logging
{
    /// <summary>
    /// Define the logging parameters
    /// </summary>
    public static void DefineLogger()
    {
        var builder = new ConfigurationSourceBuilder();

        string loggerPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

        builder.ConfigureLogging()
               .WithOptions
                 .DoNotRevertImpersonation()
               .LogToCategoryNamed("LogWriter")
               .WithOptions.SetAsDefaultCategory()
                 .SendTo.RollingFile("Rolling Flat File Trace Listener")
                 .RollAfterSize(1000)
                   .FormatWith(new FormatterBuilder()
                     .TextFormatterNamed("Text Formatter")
                       .UsingTemplate(@"Timestamp: {timestamp}{newline}Message: {message},{newline}Category: {category},{newline}Severity: {severity},{newline}Title:{title},{newline}ProcessId: {localProcessId},{newline}Process Name: {localProcessName},{newline}Thread Name: {threadName}"))
                       .ToFile(loggerPath + Properties.Settings.Default.LogFileFolder);

        // Reference app.config
        using (DictionaryConfigurationSource configSource = new DictionaryConfigurationSource())
        {
            builder.UpdateConfigurationWithReplace(configSource);

            Logger.SetLogWriter(new LogWriterFactory(configSource).Create());
        }
    }
}

Finally in any old exception:

Logger.Write("This is my log");

I hope this helps save alot of time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!