In Blazor how do I get Logger talking to ApplicationInsights

David Thielen 3,211 Reputation points
2023-06-28T22:34:54.2733333+00:00

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?

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | Blazor
{count} votes

1 answer

Sort by: Most helpful
  1. David Thielen 3,211 Reputation points
    2023-06-29T18:50:42.37+00:00

    Here's what I did to get it working:

    2. NuGet - Microsoft.ApplicationInsights.AspNetCore
    3. Azure | Create Resource | Application Insights | Create
    	1. [Go to resource] | copy Connection String (upper right)
    4. Program.cs
    	1. builder.Logging.ClearProviders(); // if want only Application Insights
    	2. builder.Logging.AddApplicationInsights();
    	3. builder.Services.AddApplicationInsightsTelemetry();
    5. appsettings.json
    	1. "ApplicationInsights": {"ConnectionString": "the connection string "},
    	2. Under "Logging": {
    		1. "ApplicationInsights": {"LogLevel" ... },
    6. To view
    	1. Logs | traces (in query editor)
    		1. Also look at exceptions
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.