ASP.NET Core log correlation in Azure

January 19th 2024 Logging .NET Azure

If you're hosting your ASP.NET Core application in Azure, you're most likely collecting your logs in Application Insights. Make sure you configure the logging in your application correctly to take advantage of built-in support for per-request log correlation.

Log correlation allows you to easily find all the log entries originating from the same request, which can be very useful when investigating issues in your application. All log entries from the same request will have the same operation ID value. You can read it from any of the log entries and then use it in a query:

traces
| where operation_Id == "48d40c790c658e8e2f7c91b2bc095184"

The result will be all the log entries from that same request:

Per-request logs in Application Insights

There are 3 ways to send your application logs to Application Insights, but not all of them support per-request log correlation.

If you only want to collect your application logs, but not any other metrics, you might be tempted to use the Application Insights logging provider. However, that's not a good idea as it doesn't have any built-in support for per-request log correlation, so you'd be giving up on this very useful feature.

You should use the Application Insights SDK for ASP.NET Core instead. You can set it up in only a few steps:

  • Install the Microsoft.ApplicationInsights.AspNetCore NuGet package.
  • Register the SDK for dependency injection:

    builder.Services.AddApplicationInsightsTelemetry();
    
  • Copy the Application Insights connection string from the Azure Portal to your appsettings.json file (the setting name is defined by the SDK):

    {
      "ApplicationInsights": {
        "ConnectionString": "<YourConnectionString>"
      }
    }
    
  • Explicitly configure the logging levels for Application Insights in appsettings.json to override the default filter that captures only logs with Warning or higher severity:

    {
      "Logging": {
        "ApplicationInsights": {
          "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning",
            "Microsoft.AspNetCore.Hosting.Diagnostics": "Information",
            "Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
          }
        }
      }
    }
    

Alternatively, you can use the Azure Monitor OpenTelemetry for ASP.NET Core. The setup procedure is very similar:

  • Install the Azure.Monitor.OpenTelemetry.AspNetCore NuGet package.
  • Register its services for dependency injection:

    builder
      .Services.AddOpenTelemetry()
      .UseAzureMonitor(options =>
      {
        options.ConnectionString =
          builder.Configuration["ApplicationInsights:ConnectionString"];
      });
    
  • Copy the Application Insights connection string from the Azure Portal to your appsettings.json file (you define the setting name with the registration code above):

    {
      "ApplicationInsights": {
        "ConnectionString": "<YourConnectionString>"
      }
    }
    
  • The OpenTelemetry logger will use your default log level configuration:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning",
          "Microsoft.AspNetCore.Hosting.Diagnostics": "Information",
          "Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
        }
      }
    }
    

You can find a working sample application with full source code in my GitHub repository. To try it out yourself, you'll have to create your own Application Insights resource in Azure and add its connection string to the appsettings.json file as described above.

Log correlation is a very important or even crucial part of efficient logging. It's very easy to get working when logging to Application Insights from ASP.NET Core applications. You only need to choose the right SDK: Application Insights SDK for ASP.NET Core or Azure Monitor OpenTelemetry for ASP.NET Core instead of the basic Application Insights logging provider.

Get notified when a new blog post is published (usually every Friday):

Copyright
Creative Commons License