且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 .NET Core 3 中使用 NLog 管理日志配置

更新时间:2023-02-16 16:22:18

决定使用 NLog 的人通常还希望禁用所有 MEL 过滤,以避免与两个过滤系统混淆.所以NLog wiki-tutorial 是针对这些用户的.

People that decide to use NLog usually also want to disable all MEL-filtering to avoid the confusion with two filtering systems. So the NLog wiki-tutorial is targeted those users.

我猜首先是 MEL 用户的人可能只会使用 new HostBuilder().CreateDefaultBuilder().Build()(将在启用所有枪支的情况下设置所有内容).

I guess people who are MEL-users first will probably just use new HostBuilder().CreateDefaultBuilder().Build() (Will setup everything with all guns enabled).

但是如果继续使用简单的示例,那么您需要删除:

But if staying with the simple example, then you need to remove:

loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);

并添加:

loggingBuilder.AddConfiguration(config.GetSection("Logging"));

所以它看起来像这样:

serviceCollection.AddLogging(loggingBuilder =>
{
   loggingBuilder.ClearProviders();
   loggingBuilder.AddConfiguration(config.GetSection("Logging"));
   loggingBuilder.AddNLog(config);
})

ILoggingBuilder.AddConfiguration 可以在 Nuget 找到:Microsoft.Extensions.Logging.Configuration

ILoggingBuilder.AddConfiguration can be found at Nuget: Microsoft.Extensions.Logging.Configuration