Logging Net Core App to Debug window

Reading time: 3 minutes

Description

In this post I will explain how to setup logging so that output messages shows in one of the debug windows i.e. Output.

What is required

  • Visual Studio
  • Net Core Web App

Let’s start by creating a Net Core App

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

Then add the name and specify the location.

As for template select Web Api.

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.AddDebug();
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

Below is how your Program.cs should look like.

Next step is to edit a logger code

Open up generated controller file. Make sure you got ILogger injected through a constructor. Then add the logger LogInformation method to the controller Get method.

As part of this step let’s add am Output window. Go to View -> Output and this should show an Output Tab.

Next thing is to Debug this app

Make sure you got Debug Configuration selected. Then either click F5 or Click the IIS Express button. Also for convenience sake untick Enable SSL for this web project in project Properties.

As you can see in the image the Output window shows our logger message.

Conclusion

Since Net Core SDK has a default Logging provider it is very easy to log stuff.

Logging is a powerful tool when dealing with development issues. Moreover it is a friend to a programmer. Because with the correctly used logging approach you have more information regarding running processes in your application.

Normally after analyzing logs you can get a hint as to where to look for bugs. It could be a real time saver.

Logging Net Core App to Azure App Service logs Filesystem

Reading time: 8 minutes

Description

In this post I will explain how to setup logging for Net Core Web App with logging messages to be monitored on Azure App Service.

What is required

  • Azure Subscription
  • Visual Studio
  • Net Core Web App

Let’s start by creating a Net Core App

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

Then add the name and specify the location.

You can select whatever web application template you want. To keep the habit of using a modern framework for frontend I will select an Angular template.

Next thing is to add Nuget package for Azure App Service Logging

Right click on the project and select Manage Nuget Packages. In that tab search for Microsoft.Extensions.Logging.AzureAppServices

Next we need to modify Program.cs file

By doing so we are adding logging to our application that would be monitored with Azure App Service for this application.

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                      // To override the default set of logging providers added 
                      // by Host.CreateDefaultBuilder, call ClearProviders
                      logging.ClearProviders();
                      // add the required logging providers
                      logging.AddAzureWebAppDiagnostics();
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

This is how the Program.cs file should look like.

What you need to decide is the Log level that you will be logging. One of the main things with logging is making sure you are logging the appropriate information. For instance Debug level would be too much for production. Moreover it can expose some sensitive information. The amount of logging in production should only be necessary information which would not affect the performance of the application. More information here.

For this case let’s leave default settings in place. This means Information log level will be logged.

Let’s modify an existing controller that was created for us. Make sure you got ILogger injected to your constuctor. ILogger comes with Net Core SDK and this is a minimum you need to do.

Next step is to Publish this application to Azure App Service

Same as when you added Nuget package in the previous step right click on the web application project and select Publish.

On the next screen select Azure.

Then select Azure App Service (Windows).

On the next screen select the appropriate subscription. After that click on Create a new Azure App Service.

On newly opened pop out select your subscription. For the Resource group and Hosting Plan add new ones. In my case since I am on a Free Subscription I will choose a free tier plan.

After you click Create the pop out will close and you will get back to the App Service details window with the new Resource group selected. Click Finish.

On the Publish tab you need to find the Publish button and click it. This will actually trigger the Publish process. You are also provided with a URL for this app. Once the app is published you will be able to see your active app.

You can monitor publish progress in Visual Studio Output window.

Next step is to activate logging on Azure

Go to azure portal home and select App Services.

On the next page select the name of your newly created App Service. In my case it will be NetCoreAzureLogging20200601230511

On that page scroll to the Monitoring section and select App Service logs.

Use the toggle button and move toggle to option On. For Level choose Information. Click Save.

Alright let’s manually test what we made here. Since it’s a HttpGet Api method we can use any browser. Enter web app URL into address bar with controller name prefix. It should read yourAppServiceUrl/yourControllerName. In my case it is

https://netcoreazurelogging20200601230511.azurewebsites.net/WeatherForecast

If we go back to the Azure portal to your App Service. Scroll to Monitoring and select Log Stream.

Once you are there you should see a line with Information Log level with the message we added to the controller.

Conclusion

As you can see setting up Azure logging is quite simple. What is more important is to think what level to log, where to store it and what exactly is required for each environment.

Logging is one of those things that can help you and on the other hand be a culprit of slower application.

It is certainly useful to have with every application as this would be an additional assistance for any case that requires monitoring. So please use it with caution.