Advertisement
Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
Feb 12

Application Insights: Telemetry logging in ASP.NET Core

Автор:
Источник:
Просмотров:
1549

Application Insights - Telemetry
Application Insights - Telemetry

1. How do I instrument (monitor/record/enabling to capture telemetry) an application?

Autoinstrumentation - if you don’t have access to source code

You only need to install the Application Insights SDK if:

The instrumentation, monitors your app and directs the telemetry data to an Application Insights resource by using a unique token. The effect on your app’s performance is small. Tracking calls are nonblocking.

Instrumentation Options - there are two different methods available:

  • Automatic instrumentation (less configurable)
  • Manual instrumentation

Manual instrumentation involves writing code that interacts with or uses Application Insights in some way.

There are two options for manual instrumentation:

Application Insights SDKs

Step 1: Create an Application Insights resource:

  1. Go to the Azure portal (https://portal.azure.com).
  2. Click on “+ Create a resource” in the upper left corner.
  3. Search for “Application Insights” and select it.
  4. Click the “Create” button and follow the steps to create a new Application Insights resource. Make sure to choose the appropriate settings for your application.

Step 2: Get the Instrumentation Key:

  1. After creating the Application Insights resource, go to the “Overview” page of your resource in the Azure portal.
  2. You will find an “Instrumentation Key” on this page. Copy this key; you will need it in the next steps.

2. Enable Application Insights server-side telemetry

Step 1. Install the Application Insights SDK NuGet package for ASP.NETCore
Step 2. Add AddApplicationInsightsTelemetry() to your Startup.cs or Program.cs class. The choice depends on your .NET Core version.
Step 3. Add builder.Services.AddApplicationInsightsTelemetry(); after the WebApplication.CreateBuilder(); method in your Program class, as in this example:

Example code (Program.cs)
Example code (Program.cs)

Step 4. Set up the connection string. Although you can provide a connection string as part of the ApplicationInsightsServiceOptions argument to AddApplicationInsightsTelemetry, we recommend that you specify the connection string in configuration. The following code sample shows how to specify a connection string in appsettings.json. Make sure appsettings.json is copied to the application root folder during publishing.

appsettings.json

Alternatively, specify the connection string in the APPLICATIONINSIGHTS_CONNECTION_STRING environment variable or ApplicationInsights:ConnectionString in the JSON configuration file.

Step 5. Configure the Application Insights SDK

You can modify a few common settings by passing ApplicationInsightsServiceOptions to AddApplicationInsightsTelemetry, as in this example:

Application Insights extension configuration

Available options
Available options

3. Add TelemetryInitializers

When you want to enrich telemetry with more information, use telemetry initializers. Add any new TelemetryInitializer to the DependencyInjection container as shown in the following code. The SDK automatically picks up any TelemetryInitializer that's added to the DependencyInjection container.

Note: it is added as Singleton.

DependencyInjection container

4. Add telemetry processors

You use telemetry processors in advanced filtering scenarios.

Telemetry configuration

5. Configure or remove default TelemetryModules

Application Insights automatically collects telemetry about specific workloads without requiring manual tracking by user.

By default, the following automatic-collection modules are enabled. These modules are responsible for automatically collecting telemetry. You can disable or configure them to alter their default behavior.

  • RequestTrackingTelemetryModule: Collects RequestTelemetry from incoming web requests.
  • DependencyTrackingTelemetryModule: Collects DependencyTelemetry from outgoing HTTP calls and SQL calls.
  • PerformanceCollectorModule: Collects Windows PerformanceCounters.
  • QuickPulseTelemetryModule: Collects telemetry to show in the Live Metrics portal.
  • AppServicesHeartbeatTelemetryModule: Collects heartbeats (which are sent as custom metrics), about the App Service environment where the application is hosted.
  • AzureInstanceMetadataTelemetryModule: Collects heartbeats (which are sent as custom metrics), about the Azure VM environment where the application is hosted.
  • EventCounterCollectionModule: Collects EventCounters. This module is a new feature and is available in SDK version 2.8.0 and later.

To configure any default TelemetryModule, use the extension method ConfigureTelemetryModule<T> on IServiceCollection, as shown in the following example:

ConfigureTelemetryModule

6. How can I track telemetry that’s not automatically collected?

Get an instance of TelemetryClient by using constructor injection and call the required TrackXXX() method on it. I don't recommend creating new TelemetryClient or TelemetryConfiguration instances in an ASP.NET Core application. A singleton instance of TelemetryClient is already registered in the DependencyInjection container, which shares TelemetryConfiguration with the rest of the telemetry.

Telemetry Client

References

Похожее
Mar 9, 2023
Author: Vithal Wadje
ASP.NET Core Blazor Server is a platform for developing modern and dynamic web applications. With Blazor Server, you can write code in C# and create rich user interfaces using HTML and CSS, all while taking advantage of server-side rendering and...
May 2, 2023
Author: Juan Alberto España Garcia
Confused about choosing between struct and class in C#? Read this article to understand the differences and make an informed decision.In C#, there are two primary object types that developers can use to build their code: structs and classes. While...
Nov 30, 2023
Author: Dev·edium
Keeping your C# applications safe and sound.Keeping app secrets safe is always tricky for developers. We want to work on the main parts of the app without getting distracted by secret-keeping. But, the app’s safety is very important. So, what...
May 13, 2023
Author: Juan Alberto España Garcia
Introduction to Async and Await in C#Asynchronous programming has come a long way in C#. Prior to the introduction of async and await, developers had to rely on callbacks, events and other techniques like the BeginXXX/EndXXX pattern or BackgroundWorker.These methods...
Написать сообщение
Почта
Имя
*Сообщение


© 1999–2024 WebDynamics
1980–... Sergey Drozdov
Area of interests: .NET Framework | .NET Core | C# | ASP.NET | Windows Forms | WPF | HTML5 | CSS3 | jQuery | AJAX | Angular | React | MS SQL Server | Transact-SQL | ADO.NET | Entity Framework | IIS | OOP | OOA | OOD | WCF | WPF | MSMQ | MVC | MVP | MVVM | Design Patterns | Enterprise Architecture | Scrum | Kanban
LinkedIn
Boosty
Donate to support the project
GitHub account
GitHub profile