Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

netcore not logging to console

I have a simple test project to experiment with logging with NetCore 2.0 using the Microsoft Logging Extensions package.

The problem I'm having is that when I run my app the very first time, it logs informational messages as expected. The weird behavior I'm having though is that subsequent runs won't produce any messages at all.

My project targets the Net Core 2.0 framework, and has the following NuGet packages installed:

  • Microsoft.Extensions.Logging
  • Microsoft.Extensions.Logging.Abstractions
  • Microsoft.Extensions.Logging.Console
  • Microsoft.Extensions.Logging.Debug

Below is my sample code I'm trying to get logging working with:

using System;

namespace LoggingNetCore2
{
    using Microsoft.Extensions.Logging;

    class Program
    {
        static void Main(string[] args)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddConsole();
            loggerFactory.AddDebug(); // <-- why is this needed for console logging?

            var logger = loggerFactory.CreateLogger(typeof(Program));
            logger.LogInformation("Hello, World!");
            logger.LogTrace("trace");
            logger.LogDebug("debug");
            logger.LogWarning("warning");
            logger.LogCritical("critical");
            logger.LogError("errrrr");

            //using (logger.BeginScope("MyMessages"))
            //{
            //    logger.LogInformation("Beginning Operation...");
            //    logger.LogInformation("Doing something cool... please wait.");
            //    logger.LogInformation("Completed successfully.");
            //}
        }
    }
}

I get no output on the console window when I run the above. Any ideas that spring to mind on what could be going on?

Things I've tried:

  • Added the Microsoft.Extensions.Logging.Abstractions package, as I vaguely remember needing to do that in NetCore 1.0 apps in the past.
  • Uninstalled / reinstalled the above packages.
  • I've even tried running the app from the command line via the dotnet.exe command, but to no avail.
  • If I downgrade to NetCore framework 1.1 (and the NuGet packages to the 1.1 versions), things work as expected.

Edit: I got logs showing now in the console window if I add the debug logging provider into the mix.

In a NetCore 1.x app, simply adding the console provider was enough.

Edit #2: Turns out the console logging provider doesn't immediately flush the messages to the console like it did in the net-core-1.x versions. It appears to run on a different thread. See this web page for info: https://github.com/aspnet/Logging/issues/631

like image 418
ajawad987 Avatar asked Sep 13 '17 01:09

ajawad987


People also ask

What is the use of ILogger in .NET Core?

ILoggerFactory is a factory interface that we can use to create instances of the ILogger type and register logging providers. It acts as a wrapper for all the logger providers registered to it and a logger it creates can write to all the logger providers at once.

How do I open the console in .NET Core?

Regardless, you can still see the console output in the Output pane in Visual Studio. There's a drop down there called "Show output from" and one of the choices is "ASP.NET Core Web server". Select this and you'll see all the console output.


1 Answers

@ajawad987, you're right. The Dispose() works.

public class Program
{
    public static void Main(string[] args)
    {
        var services = new ServiceCollection()
            .AddLogging(config => config.AddConsole())
            .BuildServiceProvider();

        services.GetRequiredService<ILogger<Program>>()
            .LogCritical("Hello");

        ((IDisposable) services)?.Dispose();
    }
}
like image 166
Juan Carlos Puerto Avatar answered Sep 20 '22 13:09

Juan Carlos Puerto