/// <summary>
        /// Constructs an instance of DependencyRegistry
        /// </summary>
        public DependencyRegistry(ConfigSettings configSettings)
        {
            string logFileTemplate = configSettings.LogFileTemplate ?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "default.log");
            string dataDirectory = configSettings.DataDirectory ?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "configdata");
            LogLevel logLevel = GetMatchingLogLevelOrDefault(configSettings.LogLevel) ?? LogLevel.Info;

            For<IRepository<IJsonEntity<ConfigRoot>>>()
                .Singleton()
                .Use(new DurableMemoryRepository<ConfigRoot>(dataDirectory, new FileSystemFacade()));

            var config = new LoggingConfiguration();
            var fileTarget = new FileTarget();
            fileTarget.Name = "LogFile";
            fileTarget.FileName = logFileTemplate;
            config.AddTarget(fileTarget.Name, fileTarget);

            var loggingRule = new LoggingRule("*", logLevel, fileTarget);
            config.LoggingRules.Add(loggingRule);
            
            LogManager.Configuration = config;

            For<ILogger>()
                .Singleton()
                .Use(l => new LoggerAdapter(LogManager.GetLogger(GetType().Namespace)));

            // Ask StructureMap to always do property injection for certain properties
            // TODO: remove this?
            SetAllProperties(policy => policy.OfType<ILogger>()); 
        }
Пример #2
0
        /// <summary>
        /// Configures OpenRasta and StructureMap for the Habitat Server.
        /// </summary>
        public void Configure()
        {
            try
            {
                using (OpenRastaConfiguration.Manual)
                {
                    ResourceSpace.Has.ResourcesOfType<ConfigRoot>()
                        .AtUri("Config/{componentName}").And
                        .AtUri("Config")
                        .HandledBy<ConfigHandler>()
                        .TranscodedBy<ConfigCodec>()
                        .ForMediaType(MediaType.Json);

                    ResourceSpace.Has.ResourcesOfType<List<string>>()
                        .AtUri("Config")
                        .HandledBy<ConfigHandler>()
                        .TranscodedBy<ConfigListCodec>()
                        .ForMediaType(MediaType.Json);
                }


                if (_container == null)
                {
                    ConfigSettings configSettings = new ConfigSettings();
                    configSettings.DataDirectory = ConfigurationManager.AppSettings["DataDirectory"];
                    configSettings.LogFileTemplate = ConfigurationManager.AppSettings["LogFileTemplate"];
               
                    int logLevel;
                    if (int.TryParse(ConfigurationManager.AppSettings["LogLevel"], out logLevel))
                    {
                        configSettings.LogLevel = logLevel;
                    }

                    _container = new Container(new DependencyRegistry(configSettings));
                }

                ResourceSpace.Uses.Resolver.AddDependencyInstance(typeof(IContainer), _container, DependencyLifetime.Singleton);
            }
            catch (Exception exception)
            {
                
            }
        }