How to Add Logging to Your Azure Web App

Adding logging to Azure App Service seems like an easy enough task, but the information that can be found from the web about this simple task always seems to miss some detail.

Logging is a critical piece of any production application. It not only helps during the development phase for debugging but is also essential in monitoring production issues in real time. In this post, we’ll walk through configuring application logging for an Azure Web App, integrating it with ASP.NET Core’s logging interfaces, and accessing the logs both live and historically.

1. Enable App Service Logs in the Azure Portal

The first step is to enable application logging through the Azure portal:

  • Navigate to Azure → Your Web App → App Service Logs.
  • Under Application logging (Filesystem), switch the setting to On.
  • Under Web server logging, switch the setting to File System

Enabling filesystem logging ensures your application writes logs to a location Azure can monitor and further process.

2. Configure Logging in Your ASP.NET Core Application

Next, you need to add Azure-specific logging diagnostics to your application. In your Program.cs file, add the following line during the builder configuration:

builder.Logging.AddAzureWebAppDiagnostics();

This command registers the Azure Web App diagnostics provider, which integrates with the built-in ASP.NET Core logging system. It ensures that logs written by your application are forwarded to Azure’s logging infrastructure.

3. Utilize ILogger in Your Code

To write logs in your application, it’s best practice to use the ILogger interface. An easy way to use ILogger everywhere is to make a global logger as follows:

Create a new file called Global.cs:

public class Global
{
	public static ILogger Logger { get; private set; }

	public static void Initialize(ILogger<Global> logger)
	{
		Logger = logger;
	}
}

In Progam.cs:

using (var scope = app.Services.CreateScope())
{
    var logger = scope.ServiceProvider.GetRequiredService<ILogger<Global>>();
    Global.Initialize(logger);
}

With this setup, you can use Global.Logger in any part of your application to log messages without needing to resolve the logging service. You can also use dependency injection if you don’t want to use global logger.

4. View Logs in Real-Time

Azure provides a feature called Log Stream that displays your application logs in real time. To use this, navigate back to the Azure portal and select:

  • Log stream → Application logs

This live stream is particularly helpful when debugging or monitoring your app’s behavior as it happens.

Retrieve Historical Logs from Kudu

Sometimes, it’s necessary to examine logs after the fact. Azure’s Kudu environment lets you browse and download logs:

  • Go to the Your Web App => Advanced Tools => Debug console => Powershell
  • Navigate to LogFiles/Application/ and look for files in the format diagnostics-YYYYMMDD.txt

This method will let you retrieve logs stored on a specific day, allowing you to perform postmortem analysis or track down issues that occurred in the past.

Happy logging!