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.

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: