Azure by Doug

Configurations for Application Insights


...see more

Telemetry Initializers set context properties that are sent along with every item of telemetry.

You can write your own initializers to set context properties.

The standard initializers are all set either by the Web or WindowsServer NuGet packages and can be found at ApplicationInsights.config reference - Azure - Azure Monitor | Microsoft Docs.

Examples of standard initializers are:

...see more

AddApplicationInsightsTelemetry() and UseApplicationInsights() both are ways to add instrumentation capabilities to your application. These are "mutually exclusive" and you should use only one of them.

UseApplicationInsights() will automatically read and initialize configuration variables from appsettings.json and is the easiest way to get started with default settings.

AddApplicationInsightsTelemetry() is recommended for customized configuration.

...see more

Use a custom Telemetry initializer to assign additional context properties you wish to populate.

Sample code below

public class MyCustomTelemetryPropertyInitializer : ITelemetryInitializer
{
    IHttpContextAccessor httpContextAccessor;

    public MyCustomTelemetryPropertyInitializer(IHttpContextAccessor httpContextAccessor)
    {
        this.httpContextAccessor = httpContextAccessor;
    }

    public void Initialize(ITelemetry telemetry)
    {
        telemetry.Context.GlobalProperties.Add("MyApplicationName", "ApplicationInsightsTester");
    }
}

You will then be able to query in Log Analytics using the below query

requests
| take 100 
| where customDimensions["MyApplicationName"] == "ApplicationInsightsTester"
...see more

A good logging configuration helps you troubleshoot problems quickly while avoiding unnecessary telemetry and storage costs. In ASP.NET Core, Application Insights is configured in two parts: one for connecting to Azure and another for controlling which log messages are collected.

Configure the Application Insights Connection

The ApplicationInsights section contains the connection to your Azure Application Insights resource.

{
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=...;IngestionEndpoint=..."
  }
}

Using a Connection String is the recommended approach and replaces the older Instrumentation Key.

Configure Logging

The Logging section determines which messages are written by your application. In most cases, the global LogLevel settings are sufficient. If needed, you can also define provider-specific settings for Application Insights.

{
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=...;IngestionEndpoint=..."
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "YourCompany.YourApplication": "Information",
      "Microsoft": "Warning",
      "System": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information",
        "YourCompany.YourApplication": "Information",
        "Microsoft": "Warning",
        "System": "Warning"
      }
    }
  }
}

What Each Setting Does

Setting Purpose
ApplicationInsights:ConnectionString Connects the application to your Azure Application Insights resource.
Logging:LogLevel Defines the default log levels used throughout the application.
Logging:ApplicationInsights:LogLevel Optionally overrides the log levels used only by the Application Insights logging provider.

Recommended Production Configuration

A balanced production configuration is usually:

  • Default: Information
  • Your application namespace: Information
  • Microsoft: Warning
  • System: Warning
  • Microsoft.Hosting.Lifetime: Information

For development, you can temporarily change your own application's namespace to Debug or Trace to collect more detailed diagnostic information without increasing the verbosity of framework logs.

This configuration provides useful application telemetry, keeps framework logging under control, and makes troubleshooting easier while avoiding unnecessary noise and storage costs.

Comments