Beispiel #1
0
        public ILogDispatcher Resolve()
        {
            if (LogDispatcherType == null)
            {
                throw new InvalidOperationException("Type of log dispatcher not set");
            }

            if (_useSingleton && _logDisptacherIntance != null)
            {
                return(_logDisptacherIntance);
            }

            foreach (var constructor in LogDispatcherType.GetConstructors())
            {
                try
                {
                    var constructorParams = constructor
                                            .GetParameters()
                                            .Select(x => _unityContainer.Resolve(x.ParameterType))
                                            .ToArray();

                    _logDisptacherIntance = constructor.Invoke(constructorParams) as ILogDispatcher;
                    if (_logDisptacherIntance != null)
                    {
                        break;
                    }
                }
                catch
                {
                    // could not construct, try another one
                }
            }

            if (_logDisptacherIntance == null)
            {
                throw new InvalidOperationException("Could not resolve the log dispatcher");
            }

            foreach (var t in _loggerTypes)
            {
                _logDisptacherIntance.Add((ILogger)_unityContainer.Resolve(t));
            }

            return(_logDisptacherIntance);
        }