Configure Azure Blob Storage as Application Log Storage
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.
- Go to Microsoft Azure portal.
- Navigate to your ASP.NET Core App Service instance.
- Click App Service logs in Monitoring section:
- Toggle setting Application Logging (Blob) on. As part of this, an extension named ASP.NET Core Logging Integration will be installed.
- Configure logging level. There are four levels that can be configured:
- Error
- Warning
- Information
- Verbose
- Choose Azure blog storage container.
- 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.
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:
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:
- Add package https://www.nuget.org/packages/Microsoft.Extensions.Logging.AzureAppServices to your project.
- In Program.cs, enable Azure Web App Diagnostics:
using Microsoft.Extensions.Logging; public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging(builder => builder.AddAzureWebAppDiagnostics()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
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}";
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().