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