如何使用ConsoleLoggerProvider创建LoggerFactory?

时间:2018-12-09 09:09:09

标签: .net-core asp.net-core-2.2

ConsoleLoggerProvider有四个构造函数:

  1. ConsoleLoggerProvider(IConsoleLoggerSettings)
  2. ConsoleLoggerProvider(IOptionsMonitor<ConsoleLoggerOptions>)
  3. ConsoleLoggerProvider(Func<String,LogLevel,Boolean>, Boolean)
  4. ConsoleLoggerProvider(Func<String,LogLevel,Boolean>, Boolean, Boolean)

其中三个消息已被废弃:

  

此方法已过时,将在以后的版本中删除。推荐的替代方法是使用LoggerFactory配置过滤,并使用ConsoleLoggerOptions配置日志记录选项。

使用构造函数#3,使用LoggerFactory创建ConsoleLoggerProvider很简单(如Entity Framework Core - Logging所述):

var loggerFactory = new LoggerFactory(new[] { new ConsoleLoggerProvider((category, level) => level >= LogLevel.Information, true) });

但是,由于不赞成使用它,所以我们只剩下构造函数#2。这是我发现等同的东西:

var configureNamedOptions = new ConfigureNamedOptions<ConsoleLoggerOptions>("", null);
var optionsFactory = new OptionsFactory<ConsoleLoggerOptions>(new []{ configureNamedOptions }, Enumerable.Empty<IPostConfigureOptions<ConsoleLoggerOptions>>());
var optionsMonitor = new OptionsMonitor<ConsoleLoggerOptions>(optionsFactory, Enumerable.Empty<IOptionsChangeTokenSource<ConsoleLoggerOptions>>(), new OptionsCache<ConsoleLoggerOptions>());
var loggerFactory = new LoggerFactory(new[] { new ConsoleLoggerProvider(optionsMonitor) }, new LoggerFilterOptions { MinLevel = LogLevel.Information });

这似乎太过复杂了,我想念一些简单的东西吗?

3 个答案:

答案 0 :(得分:18)

在.NET Core 2.2中,您可以通过Microsoft的依赖项注入框架来构建ILoggerFactory,而无需使用过时的方法。它比手动构建所有内容的版本要复杂一些。方法如下:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

IServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(builder => builder
    .AddConsole()
    .AddFilter(level => level >= LogLevel.Information)
);
var loggerFactory = serviceCollection.BuildServiceProvider().GetService<ILoggerFactory>();

在.NET Core 3.0中,您将可以使用LoggerFactory.Create

var loggerFactory = LoggerFactory.Create(builder => {
        builder.AddFilter("Microsoft", LogLevel.Warning)
               .AddFilter("System", LogLevel.Warning)
               .AddFilter("SampleApp.Program", LogLevel.Debug)
               .AddConsole();
    }
);

答案 1 :(得分:3)

以防万一,如果有人想在 efcore 的 asp.net 核心组合根中执行此操作:

services.AddDbContext<DbContext>(opt => {
    opt.AddSqlServer(Configuration.GetConnectionString("DefaultConnection"));
    opt.UseLoggerFactory(LoggerFactory.Create(builder => { builder.AddConsole(); }));
});

答案 2 :(得分:1)

@ 0xced,感谢您解构的示例,因为委托人无法在Powershell中工作,这有助于我在Powershell中执行相同的操作:

$optionsFactory = [OptionsFactory[ConsoleLoggerOptions]]::new(
    [List[ConfigureNamedOptions[ConsoleLoggerOptions]]]@(
        [ConfigureNamedOptions[ConsoleLoggerOptions]]::new('',$null)
    ),
    [List[IPostConfigureOptions[ConsoleLoggerOptions]]]::new()
)

$optionsMonitor = [OptionsMonitor[ConsoleLoggerOptions]]::new(
    $optionsFactory,
    [List[IOptionsChangeTokenSource[ConsoleLoggerOptions]]]::new(),
    [OptionsCache[ConsoleLoggerOptions]]::new()
)

$consoleLoggerProvider = [ConsoleLoggerProvider]$OptionsMonitor
$consoleLoggerProviderList = [List[ILoggerProvider]]::new()
$consoleLoggerProviderList.add($consoleLoggerProvider)

$loggerFactory = [LoggerFactory]::new(
    $consoleLoggerProviderList,
    [LoggerFilterOptions]@{
        MinLevel = [LogLevel]::Information
    }
)