feat: 添加模块初始化时间统计

main
NoahLan 9 months ago
parent 51f30f873a
commit ba04878b29

@ -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<IModuleLifecycleContributor> _lifecycleContributors;
private readonly ILogger<NPinModuleManager> _logger;
public NPinModuleManager(IModuleContainer moduleContainer, ILogger<NPinModuleManager> logger,
IOptions<AbpModuleLifecycleOptions> options, IServiceProvider serviceProvider) : base(
moduleContainer, logger, options, serviceProvider)
{
_moduleContainer = moduleContainer;
_logger = logger;
_lifecycleContributors = options.Value.Contributors.Select(serviceProvider.GetRequiredService)
.Cast<IModuleLifecycleContributor>().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]==========");
}
}
Loading…
Cancel
Save