ASP Net Core Logging with Azure
May 4, 2024
I’ve recently started my journey on ASP.NET and .NET development. Coming from a Node development environment, I was quite pleased to get to know the countless tools and middleware supported by the platform and the DI framework itself. The flexibility provided is great but this also means that configuration can be at times obscure and getting to understand what happens under the hood can be sometimes tricky.
One area it is taking time to grasp is how logging works and what are the actors involved. While it’s easy to get up and running, having a complete, production environment area can be tricky.
As a learner, the first thing I need to understand is at what level the concept I’m learning apply:
- At language level, provided by C#
- At the development platform level, provided by .NET
- At framework level, provided by ASP.NET Core
All of these are mature component of a flourish ecosystem, and most of the times what you need is to look at specific middleware provided by the framework itself.
At lower level it’s possible to integrate logging and tracing libraries, but today we want to find the easiest and most complete solution to configure.
What do we want to achieve?
We need:
- Logging, Tracing and Events support
- Integration with Application Insights
- Integration with the local development enviroment (Visual Studio)
- A simple way to divert our logs into other platform easily
Requests should be tracked automatically, as well as dependencies call and custom logs. We also want clean logs that are not polluted by useless info.
The setup
The application is a very simple AspNetCore application. The app is composed by the startup class (Program.cs), a controller, a service and that’s it.
The controller and the service are injected by an ILogger class and some logs are written whenever a request is made. Whenever the controller is invoked, a warning message is issued while whenever the service is invoked a log message is issued.
1using MatteoWebApplication.Services;2using Microsoft.AspNetCore.Mvc;34namespace MatteoWebApplication.Controllers5{6 [ApiController]7 [Route("[controller]")]8 public class WeatherForecastController : ControllerBase9 {1011 private readonly ILogger<WeatherForecastController> _logger;12 private readonly WeatherForecastService _service;1314 public WeatherForecastController(ILogger<WeatherForecastController> logger, WeatherForecastService service)15 {16 _logger = logger;17 _service = service;18 }1920 [HttpGet(Name = "GetWeatherForecast")]21 public IEnumerable<WeatherForecast> Get()22 {23 _logger.LogWarning(_logger.ToString());24 return _service.GetWeatherForecast();25 }26 }27}
Let’s install the dependencies (from this tutorial)
Microsoft.ApplicationInsights.AspNetCore
And include this line to enable the sending of the logs
1builder.Services.AddApplicationInsightsTelemetry();
What happens now?
Terminal looks clear but it lacks info about the requests

Visual studio shows requests, traces and metrics

AppInsights on Azure shows nothing. Why? Because we forgot to set the environment variable!
Let’s set:
1APPLICATIONINSIGHTS_CONNECTION_STRING
Ok, now we see it:


Things to note when using AddApplicationInsightsTelemetry
- The logs are limited to warning ones. On the console we can still se the others as well.
- Azure Application Insights take time to show the logs. Even if you see some of them, it takes time for all the events to be ingested and correlated.
Using OpenTelemetry
Let’s follow this documentation!
We install
dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore
And now we add:
1builder.Services.AddOpenTelemetry().UseAzureMonitor();
We see the logs in the terminal

We see nothing on Visual Studio

But I can see them on Azure Application Insights

Things to note when using UseAzureMonitor
- Logs are written by default at Information Level. This means that by default logs are more verbose if using this method.
- This doesn’t integrate with Visual Studio.
- If we forgot to add a connection string, the app simply wont start

Let’s enable both of them
What happens if I enable both of them?
Terminal is still fine

Visual Studio shows some strange dependency, with no info:

We now see plenty of dependencies on Application Insights!

It’s clear that both methods are used at the same time. Going under the Logs section on Application Insights shows that all the requests are logged twice!

In fact, if we remove AddApplicationInsightsTelemetry we end up with non-duplicated requests

Things to note when using both:
- A conflict happen, so we must be careful using both. For instance, we may have a single request under Application Insights but request are merged together. I wonder how the merging logic is actually implemented.
- Some stuff that get tracked differ, such as Name and operationId.