using System; using System.Collections.Generic; using System.Linq; using Umbraco.Core.Logging; namespace Umbraco.Core.Composing { /// /// Represents the collection of implementations. /// public class ComponentCollection : BuilderCollectionBase { private const int LogThresholdMilliseconds = 100; private readonly IProfilingLogger _logger; public ComponentCollection(IEnumerable items, IProfilingLogger logger) : base(items) { _logger = logger; } public void Initialize() { using (_logger.DebugDuration($"Initializing. (log components when >{LogThresholdMilliseconds}ms)", "Initialized.")) { foreach (var component in this) { var componentType = component.GetType(); using (_logger.DebugDuration($"Initializing {componentType.FullName}.", $"Initialized {componentType.FullName}.", thresholdMilliseconds: LogThresholdMilliseconds)) { component.Initialize(); } } } } public void Terminate() { using (_logger.DebugDuration($"Terminating. (log components when >{LogThresholdMilliseconds}ms)", "Terminated.")) { foreach (var component in this.Reverse()) // terminate components in reverse order { var componentType = component.GetType(); using (_logger.DebugDuration($"Terminating {componentType.FullName}.", $"Terminated {componentType.FullName}.", thresholdMilliseconds: LogThresholdMilliseconds)) { try { component.Terminate(); component.DisposeIfDisposable(); } catch (Exception ex) { _logger.Error(ex, "Error while terminating component {ComponentType}.", componentType.FullName); } } } } } } }