【问题标题】:netcore 2.0 not logging trace and debug when logging created in DI container在 DI 容器中创建日志记录时,netcore 2.0 不记录跟踪和调试
【发布时间】:2018-02-25 07:26:35
【问题描述】:

我有一个控制台应用程序,它将使用 DI 为应用程序内的服务提供配置(选项模式)、日志记录和其他服务引用。

我有一个问题,日志记录适用于 Info/Warning/Error/Critical,但 DebugTrace 没有显示。我已将控制台级别设置为 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


    【解决方案1】:

    ServiceCollection 上使用AddLogging() 扩展方法时,最低日志级别的默认值是不同的。你可以这样设置:

    static void Main(string[] args)
    {
        var serviceCollection = new ServiceCollection()
            .AddLogging(builder => {
                builder.SetMinimumLevel(LogLevel.Trace);
                builder.AddConsole();
                builder.AddDebug();
            });
    
        var serviceProvider = serviceCollection.BuildServiceProvider();
        var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
    
        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();
    }
    

    【讨论】:

    • 完美,谢谢!我还必须删除检索到的 loggerFactory 上的任何特定记录器工厂设置,并将其移动到日志记录构建器。您的代码暗示了这一点,但想向其他读者指出这一点。否则会发生奇怪的事情......再次感谢。米,
    猜你喜欢
    • 2017-05-26
    • 2013-11-13
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多