Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

netcore 2.0 not logging trace and debug when logging created in DI container

I have a console app that will use DI to supply configuration (Options Pattern), logging and other service references to services within the app.

I have a problem in that logging is working for Info/Warning/Error/Critical but Debug and Trace are not showing up. I have set the console level to Trace. If I just create a logger factory, all logs are shown.

It sounds like it is using defaults. For loggers created in the DI service collection, is there another way to configure the log level?

I have tried adding a dispose on the service collection as referred to in this post, Edit 2 link and no luck.

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;

            }
    }

}

like image 790
MarkD Avatar asked Sep 16 '17 06:09

MarkD


People also ask

Where does the log file get written to in .NET Core logging?

For example, the Console ILoggerProvider writes the logs to the console. This interface is used to create a custom instance of an ILogger . ILoggerFactory : This interface registers one or more ILoggerProvider instances and provides the CreateLogger() method used to create an instance of ILogger .

Where does ILogger write to?

ILogger offers provider-based functionality to write to the console, the debug window, the Windows Event Log, to Microsoft Azure Ap Services diagnostics and logs, as well as to the TraceSource and the EventSource.

What is the use of ILogger in .NET Core?

The ILoggerFactory is the factory interface for creating an appropriate ILogger type instance and also for adding the ILoggerProvider instance. The Logging API includes the built-in LoggerFactory class that implements the ILoggerFactory interface.


1 Answers

The defaults for the minimum log level are different when using the AddLogging() extension method on ServiceCollection. You can set it like this:

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();
}
like image 162
Martin Ullrich Avatar answered Sep 23 '22 17:09

Martin Ullrich