Welcome to the second part of our Microsoft Azure Insights Integration documentation series! This series will guide you through various aspects of integrating Azure Insights across different technologies and platforms. Below is an overview of the topics covered in this series:
A Step-by-Step Guide to Azure Insights Integration with .NET Core API (You’re here!)
Streamlining Microsoft Azure Insights Integration with Angular
Log Request Payloads Using Custom Events in Microsoft Azure Insights
Tracking Response Exceptions via Custom Event Logging in Azure Insights
In this second part, we will walk through the detailed steps required to integrate Azure Insights with a .NET Core API.
This guide explains how to integrate Microsoft Azure Insights into a .NET Core API. By doing so, we can monitor, diagnose, and gather critical insights into the performance and usage of the application, helping to ensure smooth and efficient operation.
Prerequisites
.NET Core Application: Backend API built using .NET Core (minimum version 3.1 or higher).
Azure Subscription: Access to Microsoft Azure services.
Azure Application Insights SDKs: For .NET.
Basic knowledge of Application Insights: Familiarity with logging and telemetry.
Step 1: Set Up Application Insights in Azure
Create Application Insights Resource in Azure:
Log in to your Azure Portal.
Navigate to Application Insights and create a new resource.
Provide a name, select the appropriate subscription and resource group, and set your region (choose the region closest to your application's hosting).
Once the resource is created, go to the Overview page and copy the Instrumentation Key (this will be needed for both backend and frontend integration).
Configure Role-Based Access Control (RBAC):
Ensure that the appropriate permissions are granted for accessing the telemetry data. Add necessary users or service accounts with read or contributor access.
Step 2: Install Application Insights SDK for .NET Core
Microsoft.ApplicationInsights.AspNetCore based on different versions of .NET:
.NET Version | Microsoft.ApplicationInsights.AspNetCore NuGet Version |
---|---|
.NET Core 2.1 | 2.8.1 - 2.14.0 |
.NET Core 3.1 | 2.14.0 and later |
.NET 5.0 | 2.16.0 and later |
.NET 6.0 | 2.18.0 and later |
.NET 7.0 | 2.20.0 and later |
.NET 8.0 | 2.21.0 and later |
Use the following command in the terminal or in Visual Studio Package Manager Console:
dotnet add package Microsoft.ApplicationInsights.AspNetCore
Step 3: Configure Application Insights in Startup.cs
Open the Startup.cs
file and add the Application Insights service in the ConfigureServices
method:
public void ConfigureServices(IServiceCollection services) { var applicationInsightsEnabled = Configuration.GetValue<bool>("ApplicationInsights:TelemetryFeatureEnabled"); if (applicationInsightsEnabled) { services.AddApplicationInsightsTelemetry(Configuration.GetSection("ApplicationInsights:ConnectionString")); } }
Step 4: Adding New Configurations to appsettings.json
In your appsettings.json
, add the Instrumentation Key and a feature flag to enable or disable the functionality:
{ "Logging": { "ApplicationInsights": { "LogLevel": { "Default": "Information", "Microsoft": "Error", "System": "Information" } } }, "ApplicationInsights": { "TelemetryFeatureEnabled": true, "ConnectionString": "YOUR_INSTRUMENTATION_KEY" } }
Section | Key/Setting | Default Value | Description |
---|---|---|---|
Logging | Configures logging behavior within the application. | ||
ApplicationInsights | Specifies Application Insights as the logging provider. | ||
LogLevel | Sets logging levels for different components. | ||
LogLevel.Default | "Information" | Logs at Information level or higher for all components (Information, Warning, Error, Trace, Debug, Critical and None.). | |
LogLevel.Microsoft | "Error" | Logs only Error-level events or higher from Microsoft components. | |
LogLevel.System | "Information" | Logs Information-level and above events from system-level components. | |
ApplicationInsights | Configuration for the Application Insights service integration. | ||
TelemetryFeatureEnabled | false | Enables telemetry data collection such as performance metrics, exceptions, etc. | |
ConnectionString | "YOUR_INSTRUMENTATION_KEY" | Placeholder for the actual Application Insights Instrumentation Key or Connection String. |
This table provides a clear overview of the JSON configuration, explaining each setting and its purpose.
Step 5: Visualizing Application Insights in Azure
The Overview section provides a quick, high-level view of key metrics, including:
Server Response Time
Failed Requests
Availability
CPU and Memory Usage
Navigate to Metrics to create custom dashboards with a variety of visualizations. You can monitor metrics like:
Request Rate: Number of incoming requests.
Dependency Call Failures: Issues in external dependencies like databases or third-party APIs.
Exceptions: Application errors logged as exceptions.
Performance: Application latency and load times.
Go to Investigate > Failures to get detailed insights into errors and exceptions occurring in your application:
Failed Requests: Examine requests that resulted in errors, including their status codes.
Dependency Failures: Track failed external calls (e.g., database, API).
Exception Logs: View detailed stack traces and error messages, helping you debug issues faster.
Use Investigate > Performance to track how efficiently your application is running:
Response Times: Get detailed information about server response times for different endpoints.
Slow Requests: Identify which requests or transactions are taking longer than expected.
CPU and Memory Usage: Monitor resource utilization to detect performance bottlenecks.
Navigate to Investigate > Availability to monitor the availability of your application or web service:
Availability Tests: Azure automatically pings your application from various locations to check its uptime.
Success Rate: Measure the percentage of successful availability tests.
Failure History: Drill down into failures with detailed logs and response times, along with geo-location insights.
Under Investigate > Transaction Search, you can track individual transactions to understand the flow of requests and responses:
Search for Specific Requests: Use filters to find specific requests by URL, status code, or operation.
Trace Logs for Transactions: Drill down into the details of any request to see full traces of each step (e.g., database call, API call).
Custom Event Search: If you’re logging custom events, you can search and analyze them here for more granular tracking of application behaviors.
To view backend application insights data, please ensure that the Server toggle is selected instead of Browser. This will enable visibility of telemetry related to server-side activities such as API requests, exceptions, dependencies, and performance metrics.
Conclusion
In this guide, we've walked through the step-by-step process of integrating Microsoft Azure Insights with a .NET Core API. By following the outlined steps, you’ve set up telemetry to monitor your application's performance, exceptions, and dependencies in real time. This integration helps you gain valuable insights into your application's health, allowing for proactive issue resolution and enhanced performance monitoring. With Azure Insights in place, you can now track critical metrics that will empower you to make data-driven decisions for your .NET Core API’s ongoing development and optimization.