3

I created a simple console application in .Net Core 3.1. I want to create a logger and print it to the console.

I have the following code

class Program
{
    public static void Main(string[] args)
    {
        var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
        var logger = loggerFactory.CreateLogger(nameof(Program));
        logger.LogInformation("some logging info");
    }
}

when I run the command dotnet run nothing is printed.

Is there something else I need to configure?

Palle Due
  • 5,929
  • 4
  • 17
  • 32
Yaniv daye
  • 73
  • 7
  • https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1 this might be helpful – Prasad Telkikar Apr 20 '20 at 08:16
  • 1
    I already read this article. I try to copy the code there but I still not printing to the console. – Yaniv daye Apr 20 '20 at 08:24
  • 1
    @Yanivdaye your code work to me ! , try to add Console.ReadLine(); after the LogInformation line and you will see the result in the console window – NajiMakhoul Apr 20 '20 at 08:51
  • It works, Why I should add Console.ReadLine() to see the result in the console window? – Yaniv daye Apr 20 '20 at 09:17
  • Because it's finished. When console applications have completed executing and return from their main method, the associated console window automatically closes. This is expected behavior. https://stackoverflow.com/questions/8868338/why-is-the-console-window-closing-immediately-once-displayed-my-output/8868363 – NajiMakhoul Apr 20 '20 at 09:56

0 Answers0