CodexBloom - Programming Q&A Platform

How to configure Azure Application Insights for real-time logging in a .NET 6 Web API?

👀 Views: 233 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-10
azure application-insights dotnet-6 C#

I'm testing a new approach and I tried several approaches but none seem to work. I'm converting an old project and I'm working on a project and hit a roadblock... I am trying to set up Azure Application Insights for my .NET 6 Web API to capture real-time logging and telemetry data. I followed the official documentation but am working with issues where the logs do not appear in the Azure portal. I added the Application Insights SDK to my project using NuGet: ```bash dotnet add package Microsoft.ApplicationInsights.AspNetCore ``` In my `Program.cs`, I configured Application Insights as follows: ```csharp var builder = WebApplication.CreateBuilder(args); builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["ApplicationInsights:InstrumentationKey"]); ``` Then, in my `appsettings.json`, I have: ```json { "ApplicationInsights": { "InstrumentationKey": "YOUR_INSTRUMENTATION_KEY" } } ``` I verified that the instrumentation key is correct. However, when I run the API, I don't see any telemetry data in the Azure portal. The only logs I see are from the Console output, and there are no exceptions or errors thrown. I also tried adding custom telemetry in my controller to test if it works: ```csharp public class WeatherForecastController : ControllerBase { private readonly TelemetryClient _telemetryClient; public WeatherForecastController(TelemetryClient telemetryClient) { _telemetryClient = telemetryClient; } [HttpGet] public IEnumerable<WeatherForecast> Get() { _telemetryClient.TrackEvent("GetWeatherForecastCalled"); // other logic... } } ``` Still, nothing appears in the Application Insights resource. Is there something I am missing in the setup, or are there additional configurations needed to ensure that Application Insights captures logs from my Web API? Any insights or debugging tips would be greatly appreciated. Any help would be greatly appreciated! I'm working on a CLI tool that needs to handle this. Any examples would be super helpful. For reference, this is a production web app. This is for a REST API running on Debian. Am I missing something obvious?