diff --git a/framework/NPin.Framework.Core/Modularity/NPinModuleManager.cs b/framework/NPin.Framework.Core/Modularity/NPinModuleManager.cs new file mode 100644 index 0000000..1533dd4 --- /dev/null +++ b/framework/NPin.Framework.Core/Modularity/NPinModuleManager.cs @@ -0,0 +1,60 @@ +using System.Diagnostics; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace NPin.Framework.Core.Modularity; + +[Dependency(ReplaceServices = true)] +public class NPinModuleManager : ModuleManager, IModuleManager, ISingletonDependency +{ + private readonly IModuleContainer _moduleContainer; + private readonly IEnumerable _lifecycleContributors; + private readonly ILogger _logger; + + public NPinModuleManager(IModuleContainer moduleContainer, ILogger logger, + IOptions options, IServiceProvider serviceProvider) : base( + moduleContainer, logger, options, serviceProvider) + { + _moduleContainer = moduleContainer; + _logger = logger; + _lifecycleContributors = options.Value.Contributors.Select(serviceProvider.GetRequiredService) + .Cast().ToArray(); + } + + public override async Task InitializeModulesAsync(ApplicationInitializationContext context) + { + _logger.LogDebug("==========模块初始化统计-跳过0ms模块=========="); + var total = 0; + var watch = new Stopwatch(); + long totalTime = 0; + foreach (var contributor in _lifecycleContributors) + { + foreach (var module in _moduleContainer.Modules) + { + try + { + watch.Restart(); + await contributor.InitializeAsync(context, module.Instance); + watch.Stop(); + + total++; + totalTime += watch.ElapsedMilliseconds; + + if (watch.ElapsedMilliseconds > 1) + { + _logger.LogDebug($"已加载模块[{module.Assembly.GetName().Name}],耗时[${watch.ElapsedMilliseconds}ms]"); + } + } + catch (Exception ex) + { + throw new AbpInitializationException( + $"初始化[{contributor.GetType().FullName}] 模块[{module.Type.AssemblyQualifiedName}] 时发生错误:{ex.Message} 详细信息", ex); + } + } + } + + _logger.LogInformation($"==========[{total}]个模块初始化执行完毕,总耗时[{totalTime}ms]=========="); + } +} \ No newline at end of file diff --git a/framework/NPin.Framework.Core/NPin.Framework.Core.csproj b/framework/NPin.Framework.Core/NPin.Framework.Core.csproj index cee5885..bf53b53 100644 --- a/framework/NPin.Framework.Core/NPin.Framework.Core.csproj +++ b/framework/NPin.Framework.Core/NPin.Framework.Core.csproj @@ -1,10 +1,10 @@ - - - - - - - - - - + + + + + + + + + +