Search code examples
asp.net-coreblazor-server-sideazure-application-insightsmicrosoft-extensions-logging

In Blazor how do I get Logger talking to ApplicationInsights


I am finding explinations like this (and others) reference methods that don't exist.

There is no builder.Logging.AddApplicationInsightsTelemetry() nor is there a builder.Services.AddBlazorApplicationInsights()

What I did try was:

var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddApplicationInsights();

and in appsettings.json I have:

 "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=***guid***;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/"
  },

When I run nothing is written to Application Insights. And my output has the following:

Application Insights Telemetry (unconfigured): {"name":"AppTraces","time":"2023-06-28T22:15:49.4249016Z","tags":{"ai.cloud.roleInstance":"Jasmine","ai.internal.sdkVersion":"il:2.22.0-533"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Now listening on: https://localhost:7229","severityLevel":"Information","properties":{"CategoryName":"Microsoft.Hosting.Lifetime","EventName":"ListeningOnAddress","OriginalFormat":"Now listening on: {address}","address":"https://localhost:7229","EventId":"14"}}}}
Microsoft.Hosting.Lifetime: Information: Now listening on: http://localhost:5068
Application Insights Telemetry (unconfigured): {"name":"AppTraces","time":"2023-06-28T22:15:49.4659785Z","tags":{"ai.cloud.roleInstance":"Jasmine","ai.internal.sdkVersion":"il:2.22.0-533"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Now listening on: http://localhost:5068","severityLevel":"Information","properties":{"CategoryName":"Microsoft.Hosting.Lifetime","EventName":"ListeningOnAddress","OriginalFormat":"Now listening on: {address}","address":"http://localhost:5068","EventId":"14"}}}}
Microsoft.Hosting.Lifetime: Information: Application started. Press Ctrl+C to shut down.
Application Insights Telemetry (unconfigured): {"name":"AppTraces","time":"2023-06-28T22:15:49.4729221Z","tags":{"ai.cloud.roleInstance":"Jasmine","ai.internal.sdkVersion":"il:2.22.0-533"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Application started. Press Ctrl+C to shut down.","severityLevel":"Information","properties":{"CategoryName":"Microsoft.Hosting.Lifetime","OriginalFormat":"Application started. Press Ctrl+C to shut down."}}}}
Microsoft.Hosting.Lifetime: Information: Hosting environment: Development
Application Insights Telemetry (unconfigured): {"name":"AppTraces","time":"2023-06-28T22:15:49.4759303Z","tags":{"ai.cloud.roleInstance":"Jasmine","ai.internal.sdkVersion":"il:2.22.0-533"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Hosting environment: Development","severityLevel":"Information","properties":{"CategoryName":"Microsoft.Hosting.Lifetime","OriginalFormat":"Hosting environment: {EnvName}","EnvName":"Development"}}}}
Microsoft.Hosting.Lifetime: Information: Content root path: C:\git\LouisHowe\LouisHowe.web
Application Insights Telemetry (unconfigured): {"name":"AppTraces","time":"2023-06-28T22:15:49.4789848Z","tags":{"ai.cloud.roleInstance":"Jasmine","ai.internal.sdkVersion":"il:2.22.0-533"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Content root path: C:\\git\\LouisHowe\\LouisHowe.web","severityLevel":"Information","properties":{"CategoryName":"Microsoft.Hosting.Lifetime","OriginalFormat":"Content root path: {ContentRoot}","ContentRoot":"C:\\git\\LouisHowe\\LouisHowe.web"}}}}

Any ideas how I can get this working?


Solution

  • Turned out to be simple, one more call. In Program.cs add:

    builder.Logging.AddApplicationInsights();
    builder.Services.AddApplicationInsightsTelemetry();