Logging Net Core App to Console

Reading time: 7 minutes

Description

In this post I will explain how to set up logging so that output messages show in the Console. There will be two options of how to do it. One in Console Application and the other with Web Application that is run as a project.

What is required

  • Visual Studio
  • Net Core Console App
  • Net Core Web App

Let’s start by creating a Net Core Web App

Open Visual Studio and choose ASP.NET Core Web Application.

Add Project name and location for this application.

Then select API for project template.

Next step is to edit Program.cs file

We need to edit a code that builds a host object. In essence it is a place where everything gets set up i.e. DI, Logging, Configuration, IHostedService implementations.

For this tutorial we are interested in Logging only.

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    // override the default set of logging providers added by Host.CreateDefaultBuilder
                    logging.ClearProviders();

                    // add the required logging providers
                    logging.AddConsole();
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

Below is how your Program.cs should look like.

Now let’s see what we made here

Before running the application for the first time we need to edit Debug Profile. First set it to our project name NetCoreLoggingConsole and then set it to launch as Project.

Now if we Run/Start Debugging the application we should see a Console Application window to appear. On startup it hits the Get method of the controller and as a result logger logs Information Log Level message. It then appears in the Console Application window. To note you need to make sure you got the ILogger interface into the constructor of the controller.

More closer image.

We can achieve the same result in the Console Application

Let’s add a new project Console Application to this solution. Right Click on the Solution name.

Select Console App (.Net Core)

Let’s configure necessary bits to add Logging to this application. For that we need to install the Microsoft.Extensions.Logging.Console nuget package.

After the nuget package is installed, let’s open Program.cs for this new project and add some code inside the Main method.

//net core default DI
            var serviceProvider = new ServiceCollection()
                .AddLogging(builder => builder
                                    .AddConsole()
                                    .SetMinimumLevel(LogLevel.Debug))
                .BuildServiceProvider();

            //create logger instance
            var logger = serviceProvider
                                .GetService<ILoggerFactory>()
                                .CreateLogger<Program>();

            logger.LogInformation("CONSOLE -> Main method");

            Console.WriteLine("Hello World!");

            Console.ReadLine();

This is how it should look like once everything is in place.

Next step is to set to run multiple projects for this solution.

There you go a closer look.

As a result we should see two Console Application windows. If you remember one for Web Application that is run as Project and one for Console Application.

And closer look.

Conclusion

Logging to Console can be handy. Normally you would develop small programs for practice purposes. However with Net Code you can implement more advanced stuff like add DI, host object, configuration etc

As a result you can have a Console Application based microservice to do a particular job. It is straightforward to set up and you can quickly develop applications following this pattern.

What is great is that you can add just necessary stuff and develop around it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: