Application Insights - 5 Tips to Improve Your Observability
Tags: observability, azure, netcore, applicationinsights
Published: 2024-05-04 14:47:25

![Grey image with a magnifier at the center and lines representing a digital circuit around it](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fs02wbd2wo2omoasftmn.jpeg)

<h3><b>Introduction</b></h3>

This article aims to bring some tips about Microsoft Application Insights that can help you enhance the observability of your applications. The code examples here used the built-in SDK for ASP.NET Core and C#

<h3><b>Azure Applications Insights</b></h3>

Azure Applications Insights is an observability tool developed by Microsoft. One of its benefits is that it aggregates distributed tracing, metrics, and logs in a single place. With a few lines of code, you can instrument your whole application. You can create customized logs and metrics, filter, and visualize them easily. 

All the data stored can be retrieved using the Kusto Query Language (KQL), which is a rich language that allows you to write complex queries and transform the results of them on Graphs or Alerts easily.

<h3 name="section2"><b>1. Log Correlation</b></h3>

When an API endpoint is called, a correlation ID value is created. The value can later be used to fetch all the information App Insights ingested during the request.  
The correlation ID should be a number or a string value, and it can be sent as part of the request Header or generated when reaching the API.

The greater benefit is allowing you to visualize the sequence of the events. Microservices architectures take big advantage of it, giving you information about all the multiple requests involved during a transaction

There are at least two ways you can add a correlation ID:

a) W3C Traceparent header key

Add a traceparent key to the requests header. The trace-id contained in the value will become the correlation ID. For more details access the <a href="https://learn.microsoft.com/en-us/azure/azure-monitor/app/distributed-trace-data/">link</a>.

b) Evolving methods within the TelemetryClient operation method. All requests made between the Start and Stop operations will receive a common correlation ID.

```csharp 
public void DoSomething()
{
    var operation = telemetryClient.StartOperation(requestTelemetry);
    
    try
    {
	// Operations
    }
    finally
    {
        // Update status code and success as appropriate.
        telemetryClient.StopOperation(operation);
    }
}
```

For more information about operations, access <a href="https://learn.microsoft.com/en-us/azure/azure-monitor/app/custom-operations-tracking">link</a>

<h3 name="section3"><b>2. Add custom dimension fields to your logs</b></h3>

Creating multidimensional logs that include information beyond a simple text message is a good practice. This helps to understand better the context in which the events were created and makes querying easier.

TelemetryClient

```csharp
public void ProcessPayment()
{
    telemetryClient.TrackTrace("Processing credit card payment", new Dictionary<string, string>()
    {
        {"CustomerId", "12345"},
        {"OrderId", "54"},
    });
}
```

ILogger

```csharp 
public void ProcessPayment()
{
   using (logger.BeginScope(new Dictionary<string, object>{
       ["CustomerId"] = 12345,
       ["OrderId"] = 54
   }))
   {
      logger.LogInformation("Processing credit card payment");
   }
}
```

<h3 name="section4"><b>3. Add the Application version</b></h3>

During Application Insights' initialization, you have the option to specify your application's version. Then, all your logs will include the version value. This is useful to identify whether an error occurred prior to or after a specific deployment.

```csharp
var options = new ApplicationInsightsServiceOptions();

options.ApplicationVersion = "1.0.0.1";

builder.Services.AddApplicationInsightsTelemetry(options);
```

<h3 name="section5"><b>4. Enable Developer Mode</b></h3>

When debugging, you can use the developer mode to send logs to Application Insights instantly. This saves time since you don't need to wait for the logs before analyzing them. However, this option should not be activated in production environments as it has a significant performance impact. 

```csharp
var options = new ApplicationInsightsServiceOptions();

options.DeveloperMode = true;

builder.Services.AddApplicationInsightsTelemetry(options);
```

<h3 name="section1" ><b>5. Customize your Instrumentation</b></h3>

The ASP.NET SDK has built-in automatic instrumentation, which allows you to instrument your whole application with a few lines of code. However, you might want to write your own metrics. 
The SDK is flexible enough to allow you to disable logs/metrics you don't want to auto-collect. For that, you just need to set the options during the API initialization.

Example:

```csharp 
var options = new ApplicationInsightsServiceOptions();

// Enable/disable metrics related to external requests. E.g. Database, service bus, and 3rd party APIs.
options.EnableDependencyTrackingTelemetryModule = false;

// Enable/Disable metrics related to all requests received by the API
options.EnableRequestTrackingTelemetryModule = false;

builder.Services.AddApplicationInsightsTelemetry(options);
```

For a full list of ApplicationInsightsServiceOptions settings, check out this <a href="https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core?tabs=netcorenew">link</a> 


<h3><b>Conclusion</b></h3>

Implementing these simple tips can significantly enhance the quality of your App. Insights logs, enabling you to quickly identify problems, resolve bugs and improve the overall reliability of your applications.