liveBook · Manning

Application insights logging provider

As explained in Building Web APIs with ASP.NET Core

The Application Insights logging provider is a .NET logging component that sends application log events and telemetry to Azure Application Insights.

The Application Insights logging provider sends .NET application log events to an Azure Application Insights service, where logs and performance-related output can be collected and analyzed. It provides a cloud-based alternative to console, debug, event-source, and event-log providers. section 7.3.3 section 7.2.4

Using it with the MyBGList web API requires an Azure account and an Application Insights instance. Copy the instance connection string into application configuration before registering the provider. section 7.3.3

Configure the provider in four practical steps:

  • Create an Application Insights instance through the Azure management experience. section 7.3.3
  • Store the retrieved connection string under Azure:ApplicationInsights:ConnectionString. section 7.3.3
  • Install Microsoft.Extensions.Logging.ApplicationInsights and Microsoft.ApplicationInsights.AspNetCore, both shown at version 2.21.0. section 7.3.3
  • Register Application Insights through builder.Logging, optionally retaining simple console and debug providers alongside it. section 7.3.3 section 7.2.4
"Azure": {
    "ApplicationInsights": {
      "ConnectionString": "<INSERT_CONNECTION_STRING_HERE>"   #1
    }
  }

Use the configuration listing as the required shape for the Azure connection string. section 7.3.3

> dotnet add package Microsoft.Extensions.Logging.ApplicationInsights --version 2.21.0

> dotnet add package Microsoft.ApplicationInsights.AspNetCore 
    --version 2.21.0

Use the package commands to add the Application Insights logging and ASP.NET Core integration libraries. section 7.3.3

builder.Logging
  .ClearProviders()
  .AddSimpleConsole()
  .AddDebug()
.AddApplicationInsights(              #1
telemetry => telemetry.ConnectionString =
builder
.Configuration["Azure:ApplicationInsights:ConnectionString"],
loggerOptions => { });

Use the logging-builder example to connect the configured connection string and keep console and debug output enabled. section 7.3.3

Verify telemetry

Run the web API and inspect Application Insights for application logs. The resulting entries are shown as TRACE event types, confirming that the provider is receiving application output. section 7.3.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Walks through Azure setup, connection-string configuration, package installation, provider registration, and telemetry verification.
Examples
Uses the MyBGList web API, Azure configuration, NuGet commands, logging-builder registration, and TRACE event output.
What it assumes
Assumes a .NET web API and ASP.NET Core logging configuration while introducing Azure account and Application Insights setup.
What you can do after
Operate/configure an ASP.NET Core application to send logs to Application Insights and verify the resulting telemetry.
How it connects
Connects ILogger, ASP.NET Core logging providers, Azure Monitor, Azure, and structured logging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.