Logging to Azure Blob Storage


Reading time: 8 minutes

Description

In this post I will explain how to set up logging for Net Core Web App with logging messages to be stored in Azure Blob Storage.

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.

On the next window give your application a name and choose where to save it.

For this application select API 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();
                })
                .ConfigureServices(serviceCollection =>
                    serviceCollection //configure Azure Blob Storage
                        .Configure<AzureBlobLoggerOptions>(options =>
                        {
                            //set the last section of log blob name
                            options.BlobName = "log08062020.txt";
                        })
                )
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

This is how the Program.cs file should look like. When we set the BlobName it is only for the last section of the name. Hence the actual name in blob storage would look something like blabla12345_log08062020.txt

Next we need to edit generated controller code

If you have chosen the API template in previous steps then this part should be a one liner code. The controller should already have an ILogger interface injected to the controller constructor. Our task is to add some code in the HttpGet method that will call ILogger method to write a log message.

_logger.LogInformation("INFORMATION -> WeatherForecastController");

Next let’s publish our application and create Azure Storage at the same time

For this we need to right click on the project and select Publish. Then you should see the Publish pop out window. There, select Azure.

On the next pop out select Azure App Service (Windows).

On the next window check your Subscription. Then click Create a new Azure App Service.

On the next window add a new Resource group and Hosting plan.

Once you have set up everything click Finish.

This is what you should see next.

We need to add Azure storage as dependency to our application. So click Add.

On the pop out select Azure Storage.

And then Create a storage account.

On the next pop out we need to select the resource group that we have added in previous steps when we create a new Azure App Service. It should be NetCoreAzureBlobLogging20200608171456Resource This way we got both web application and storage in the same group. This helps to keep things organized, you can work with them together, delete them together and so on.

For an Account type select Standard – Locally Redundant Storage. Locally redundant storage (LRS) copies your data synchronously three times within a single physical location in the primary region. LRS is the least expensive replication option, but is not recommended for applications requiring high availability.

Select our newly created storage instance and click Next.

Add connection string name, leave option to save connection string as Azure App Settings.

Finally you should see something similar. In order to actually publish you need to click the Publish button. Nothing complex but sometimes you can get distracted by so many steps and forget to actually click it. But you are clever and won’t get caught doing this.

Once it is successfully published a web page should open. Alternatively we can use Site URL in the browser of your choice. Or even we can go to Azure portal home, click on Resource Groups, then find your group created in previous steps and select App Service for our web application.

Next step is to enable logging to Azure Blob Storage

On the Azure App Service page for our web application go to the Monitoring section of left the pane menu. In there select App Service logs. Switch On Application Logging (Blob), set Level to Information, Retention Period to 1 and then click on Storage Settings.

In the next window select our blob storage account and create a container.

Let’s manually test what we got

For this you can use any Api client or debugging tool e.g. Postman, Fiddler etc However in our case because it’s a HttpGet method we can use any browser.

Put our Azure App Service web application Url into the address bar of a web browser, prefix it with /WeatherForecast as the name of our controller and hit Enter. Like it is shown below.

https://netcoreazurebloblogging20200608171456.azurewebsites.net/WeatherForecast

This is what you should see.

Next we need to go to the Blob Storage container section of Azure portal. You can find it either under the same Resource Group or you can access it through Azure portal home page.

From there click on Containers

Then you need to go all the way to our Log file location.

There should be a Download button.

Once the download has completed you can inspect the Log text file and see our logged message from the controller.

Before we wrap up we need to tidy things up

It is a good habit to stop or delete your created service once you finish playing with them. This way you would prevent any service charges. If you go back to the Resource Group section of Azure portal and select your one. On the details page of that Resource you can delete it together with all the services under it.

Conclusion

As you have noticed, for a basic setup of logging you really need to do a few steps. And again it is mostly downloading a particular Nuget package, adding a few lines of code to Program or Startup files, creating an Azure storage account and there you go!

If you want something tangible that can be applied to real world applications I recommend to looking into Structured logging.

Have fun exploring and stay tuned!

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: