最近,我将项目从2.2迁移到AspNetCore 3.1,并开始使用Visual Studio2019。此后,大约需要一分钟来启动应用程序。大部分时间此应用程序正在获取nlog.config:
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
LogManager.LoadConfiguration(System.String.Concat
(Directory.GetCurrentDirectory(), "/nlog.config")); //here's where it hangs
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
//.AddJsonFile($"rabbitConfig.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
我知道IHostingEnvironment已过时,会导致此问题吗?我应该更改什么以使其加载更快?
您可以尝试跳过对程序集的自动扫描以加载NLog扩展名:
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
//disable NLog assembly scanning
NLog.Config.ConfigurationItemFactory.Default = new NLog.Config.ConfigurationItemFactory(typeof(NLog.ILogger).GetTypeInfo().Assembly);
//here's where it hangs
NLog.LogManager.LoadConfiguration(System.String.Concat(Directory.GetCurrentDirectory(), "/nlog.config"));
...
}
也建议使用NLog 4.6.8,因为它具有加快配置加载速度的多项优化。
注意,如果您在跟踪级别启用了NLog InternalLogger,那么也会损害性能。
[请记住,如果您禁用默认的MEL-Console-Logger,或从Microsoft.Hosting.Lifetime
-Logger中过滤掉了Info-Level消息,则托管环境将超时,等待预期的启动消息。确保配置NLog-Console-output或保留AddConsole-output:
https://github.com/NLog/NLog.Web/wiki/Hosting-Lifetime-Startup-Messages
在ASP.NET Core 2中,它将直接写入控制台,但是在ASP.NET Core 3中,它将使用称为Microsoft.Hosting.Lifetime
的MEL-ILogger(记住,将MEL-Filter和NLog-Filter配置为不丢弃这些消息在ASP.NET Core 3中)