Tingle.AspNetCore.ApplicationInsights 4.13.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Tingle.AspNetCore.ApplicationInsights --version 4.13.0                
NuGet\Install-Package Tingle.AspNetCore.ApplicationInsights -Version 4.13.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tingle.AspNetCore.ApplicationInsights" Version="4.13.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tingle.AspNetCore.ApplicationInsights --version 4.13.0                
#r "nuget: Tingle.AspNetCore.ApplicationInsights, 4.13.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Tingle.AspNetCore.ApplicationInsights as a Cake Addin
#addin nuget:?package=Tingle.AspNetCore.ApplicationInsights&version=4.13.0

// Install Tingle.AspNetCore.ApplicationInsights as a Cake Tool
#tool nuget:?package=Tingle.AspNetCore.ApplicationInsights&version=4.13.0                

Tingle.AspNetCore.ApplicationInsights

This library provides some extensions for Microsoft.ApplicationInsights.

Add details of the request source to application insights telemetry

You can add some request source details to the RequestTelemetry that is sent along to application insights. The following details can be added:

  • The package name which should be found in the X-App-Package-Id request header
  • The version name which should be found in the X-App-Version-Name request header
  • The version code which should be found in the X-App-Version-Code request header
  • The User Agent which should be found in the User-Agent request header
  • The IP address

To accomplish this, add the following logic to Program.cs or Startup.cs file:

services.AddApplicationInsightsTelemetryExtras();
services.AddHttpContextAccessor();

We've injected the IHttpContextAccessor in the DI container so that we can access the HttpContext object that contains the HTTP request details.

The request source details will be seen as custom properties of an application insights telemetry record.

Add all request headers to application insights telemetry

You can send all request headers to application insights by adding the following logic to Program.cs or Startup.cs file:

services.AddApplicationInsightsTelemetryHeaders();
services.AddHttpContextAccessor();

We've injected the IHttpContextAccessor in the DI container so that we can access the HttpContext object that contains the HTTP request details.

The request headers will be seen as custom properties of an application insights telemetry record.

Add manual dependency tracking in application insights

A dependency is a component that's called by your application. It's typically a service called by using HTTP, a database, or a file system. Application Insights measures the duration of dependency calls and whether it's failing or not, along with information like the name of the dependency. You can investigate specific dependency calls and correlate them to requests and exceptions. The list of dependencies that are automatically tracked can be seen here.

So how do we assist in tracking of dependencies that aren't automatically tracked?

We do this by using ActivitySource/ActivityListener APIs, which make it quite a bit simpler to raise and listen to events for Activity start/stop.

Per conventions, the activity source name should be the name of the assembly creating the activities. That makes it much easier to "discover" activities, you don't have to expose a constant or search through source code to discern the name.

We can then create an IHostedService that uses ActivityListener internally, collects from the ActivitySources that are needed, creates instance(s) of DependencyTelemetry then sends to application insights via the TrackDependency API.

This can be accomplished by adding the following logic to Program.cs or Startup.cs file:

services.AddActivitySourceDependencyCollector(["Tingle.EventBus", "Tingle.Extensions.MongoDB"]);

You can replace the array of activities supplied in AddActivitySourceDependencyCollector(...) with your own.

Track problem details in application insights

Track the problem details in application insights when the response is a BadRequestObjectResult with value of ProblemDetails. The properties are seen as custom properties of an application insights telemetry record. To do this, annotate your controller with the TrackProblems attribute:

[TrackProblems]
[ApiVersion("1")]
[Route("v{version:apiVersion}/[controller]")]
public class DummyController : ControllerBase
{
  [HttpPost]
  public async Task<IActionResult> SendAsync([FromBody] SendRequestModel model)
  {
      ...
      // In case of a bad request
      return Problem(title: "error_title", description: "more detailed description", statusCode: 400);
      ...
    }
}
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.14.1 102 10/14/2024
4.14.0 142 9/16/2024
4.13.0 212 8/13/2024
4.12.0 152 8/7/2024
4.11.2 176 7/15/2024
4.11.1 204 6/26/2024
4.11.0 172 6/6/2024
4.10.1 111 6/5/2024
4.10.0 138 5/27/2024
4.9.0 234 5/16/2024
4.8.0 365 5/5/2024
4.7.0 601 3/25/2024