Raymond Raymond / Microsoft Azure

Configure Azure Blob Storage as Application Log Storage

event 2020-12-22 visibility 4,824 comment 2 insights toc
more_vert
insights Stats

Azure App Service is a server-less product that can be used to host websites. For ASP.NET core websites, App Service Log feature allows users to configure logging for the web application. This article will show you how to configure Azure Blog Storage container as logging storage for ASP.NET Core web applications.

Config App Service Log

Follow these steps to collect logs and save them to Azure blob storage.

  1. Go to Microsoft Azure portal.
  2. Navigate to your ASP.NET Core App Service instance. 
  3. Click App Service logs in Monitoring section: 
    2020122252211-image.png
  4. Toggle setting Application Logging (Blob) on. As part of this, an extension named ASP.NET Core Logging Integration will be installed. 

  5. Configure logging level. There are four levels that can be configured:
    • Error
    • Warning
    • Information
    • Verbose
  6. Choose Azure blog storage container.

  7. Click Save button to save changes. 

The following screenshot shows the settings for Kontext platform:

The logs are stored for 180 days in kontexttech storage account's site-logs container. 

2020122252521-image.png

View logs in blob storage

Navigate to the blob storage container, you will find the logs are organized in the following folder structure:

Year (2020) > Month (12) > Day (22) > Hour (05) > Application log file.

The following is one example log file generated on Kontext:

2020122253448-image.png

About ASP.NET Core Logging Integration

In the above steps, we utilized the features of Azure App Service to enable logging to Azure blog storage.

You can also directly enable it as part of your ASP.NET Core web application build.

To do this follow these steps:

using Microsoft.Extensions.Logging;

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(builder => builder.AddAzureWebAppDiagnostics())
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
More from Kontext
comment Comments
Raymond Raymond #380 access_time 4 years ago more_vert

Hi Felix,

Unfortunately, I don't think you can write logs to just one file as it is hard-coded:

var blobName = $"{_appName}/{key.Year}/{key.Month:00}/{key.Day:00}/{key.Hour:00}/{_fileName}";

You have to implement your own customized logger to do that. Azure Blob Storage has .NET APIs you can use to invoke in your ASP.NET core applications to write into blob storage directly. 

For example, the following article uses those APIs to write uploaded images into Azure Blob Storage and you can easily change it to write text files. 
format_quote

person Felix access_time 4 years ago

Hi, great article! 

Is it possible to change the folder structure of the logs from 

"Year (2020) > Month (12) > Day (22) > Hour (05) > Application log file."
To just one simple Logfile? Like ./logs/applicationlogfile.log

I tried several things in .ConfigureLogging()

But I'm not sure if the Azure App Service is just overwriting the settings or not. It's kind of time consuming to find a solution because it always requires a web release + appservice restart to test it... After several hours I'm kind of desperated now :) 

If they would it must work with disabling logging on the Azure portal settings and then define the destination of the logfile manually in .ConfigureLogging().

F Felix L. #379 access_time 4 years ago more_vert

Hi, great article! 

Is it possible to change the folder structure of the logs from 

"Year (2020) > Month (12) > Day (22) > Hour (05) > Application log file."
To just one simple Logfile? Like ./logs/applicationlogfile.log

I tried several things in .ConfigureLogging()

But I'm not sure if the Azure App Service is just overwriting the settings or not. It's kind of time consuming to find a solution because it always requires a web release + appservice restart to test it... After several hours I'm kind of desperated now :) 

If they would it must work with disabling logging on the Azure portal settings and then define the destination of the logfile manually in .ConfigureLogging().

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts