【发布时间】:2018-02-25 07:26:35
【问题描述】:
我有一个控制台应用程序,它将使用 DI 为应用程序内的服务提供配置(选项模式)、日志记录和其他服务引用。
我有一个问题,日志记录适用于 Info/Warning/Error/Critical,但 Debug 和 Trace 没有显示。我已将控制台级别设置为 Trace。如果我只是创建一个记录器工厂,则会显示所有日志。
听起来它正在使用默认值。对于 DI 服务集合中创建的 logger,有没有其他方法可以配置日志级别?
我已尝试在this 帖子、编辑 2 链接中提到的服务集合上添加处置,但没有运气。
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using System;
namespace DownloadClientData.App
{
class Program
{
static int Main(string[] args)
{
//***if I use this way to create the logger in the DI container, debug and trace messages are not displayed
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging();
//tried this alternative too - no change...
//serviceCollection.AddLogging(LoggingBuilder => LoggingBuilder.AddFilter<ConsoleLoggerProvider>("All Categories", LogLevel.Trace));
var serviceProvider = serviceCollection.BuildServiceProvider();
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
//***If I comment out the above lines and uncomment the below line, all 6 logs are displayed.
//var loggerFactory = new LoggerFactory();
loggerFactory
.AddConsole(LogLevel.Trace)
.AddDebug(LogLevel.Trace);
var logger = loggerFactory.CreateLogger(typeof(Program));
logger.LogInformation("Information");
logger.LogTrace("Trace");
logger.LogDebug("Debug");
logger.LogWarning("Warning");
logger.LogCritical("Critical");
logger.LogError("Error");
Console.ReadKey();
return 0;
}
}
}
【问题讨论】:
标签: logging .net-core-2.0