加载记录器配置在启动期间需要很长时间
Loading logger config takes long time during startup
最近项目从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 已经过时了,它会导致这个问题吗?我应该更改什么以使其加载速度更快?
如果您的同事没有遇到问题,那么您很可能是 Visual Studio 2019 年可笑的缓慢符号加载的受害者。一种解决方法是 Enable Just My Code
您可以尝试跳过程序集的自动扫描以加载 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-messages,那么托管环境将在等待预期的启动消息时超时。确保配置 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)
中不丢弃这些消息
检查配置中的扩展。可能库不存在,这会在加载 NLog 时明确导致此问题。
我的猜测是 NLog 正在搜索 DLL,或者如 Rolf 所说,试图加载 pdb(除非它不存在)。
<extensions>
<add assembly="NLog.Extended" />
</extensions>
在我的例子中,NLog.Extended
需要更改为 NLog.Web.AspNetCore
,因为 NLog.Extended 在 .net 核心中不存在。这归结为来自另一个项目的错误复制粘贴。
NLog 的用户应该检查他们的 nlog.config,并为他们的目标框架使用正确的示例,例如:AspNetCore 2 or AspNetCore 3
最近项目从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 已经过时了,它会导致这个问题吗?我应该更改什么以使其加载速度更快?
如果您的同事没有遇到问题,那么您很可能是 Visual Studio 2019 年可笑的缓慢符号加载的受害者。一种解决方法是 Enable Just My Code
您可以尝试跳过程序集的自动扫描以加载 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-messages,那么托管环境将在等待预期的启动消息时超时。确保配置 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)
检查配置中的扩展。可能库不存在,这会在加载 NLog 时明确导致此问题。 我的猜测是 NLog 正在搜索 DLL,或者如 Rolf 所说,试图加载 pdb(除非它不存在)。
<extensions>
<add assembly="NLog.Extended" />
</extensions>
在我的例子中,NLog.Extended
需要更改为 NLog.Web.AspNetCore
,因为 NLog.Extended 在 .net 核心中不存在。这归结为来自另一个项目的错误复制粘贴。
NLog 的用户应该检查他们的 nlog.config,并为他们的目标框架使用正确的示例,例如:AspNetCore 2 or AspNetCore 3