Tracking Response Exceptions via Custom Event Logging in Azure Insights
Welcome to the fifth and final part of our Microsoft Azure Insights Integration documentation series! This series has guided you through various aspects of integrating Azure Insights across different technologies and platforms. Belo is an overview of the topics covered in this series:
A Step-by-Step Guide to Azure Insights Integration with .NET Core API
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 (You’re here!)
In this fifth part, we will dive into tracking response exceptions using custom event logging in Microsoft Azure Insights to improve error tracking and debugging.
This guide explains how to track response exceptions via custom event logging in Microsoft Azure Application Insights. By doing so, we can monitor, capture, and analyze exception data, helping to diagnose errors and improve the reliability of backend services. This approach provides critical insights into application failures, enabling more comprehensive error tracking and troubleshooting, ultimately ensuring a smoother and more resilient application performance.
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:
{
"ApplicationInsights": {
"TelemetryFeatureEnabled": true,
"ConnectionString": "YOUR_INSTRUMENTATION_KEY"
}
}
Section | Key/Setting | Default Value | Description |
---|---|---|---|
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: Create azureinsightsconfiguration.json
for Azure Insights
To better manage Azure Application Insights settings, it's useful to place the configurations in a dedicated file. In the root or configuration folder of the project, create a new file called azureinsightsconfiguration.json
.
Configuration Name | Default Value | Definition |
---|---|---|
Exception | - | Main configuration object. |
LoggingOption |
| Specifies logging behavior: |
LogRequests | - | A list of API endpoints and HTTP methods for which logging is enabled. |
Path | - | The path of the API request to be logged. |
Method | - | HTTP method for the API request. |
DisabledDatabaseList | - | Lists the databases that are disabled. |
Database | - | Comma separated names of the disabled database. |
This table provides a clear overview of the JSON configuration, explaining each setting and its purpose.
Step 6: Configure azureinsightsconfiguration.json
in Program.cs
Open the
Program.cs
file and add theazureinsightsconfiguration.json
in theCreateHostBuilder
method:
Set up logging, error handling, and dynamic configuration loading in
Program.cs
using configuration sources likeappsettings.json
and environment variables:
Step 07: Azure Insights Middleware Implementation - AzureInsightsMiddleware.cs
Explanation: This code defines a middleware, AzureInsightsMiddleware
, to log response exceptions to Azure Application Insights in a .NET Core application. It captures exceptions during the request lifecycle and logs them as custom events.
Telemetry Check:
It first checks if telemetry logging is enabled via the configuration (
"ApplicationInsights:TelemetryFeatureEnabled"
).
Exception Handling:
If an exception occurs during the request, it is caught in a
try-catch
block.
Logging Conditions:
The middleware checks if the request path and method match the logging rules defined in the configuration (
"Exception:LogRequests"
).It also checks an option (
"Exception:LoggingOption"
) to determine whether exceptions should be logged.
Exception Data:
It collects key details such as the exception message, stack trace, and inner exceptions.
If the user is authenticated, it also logs the user's organization/database information from their claims.
Log to Azure Insights:
If conditions are met, the exception and metadata are logged as a custom event (
"Response Exception"
) to Azure Application Insights.
This middleware allows automated logging of detailed exception data for better monitoring and debugging in Azure.
Step 08: Register the Middleware in Startup.cs
Open the Startup.cs
file and add the AzureInsightsMiddleware
in the Configure
method:
Ensure that the AzureInsightsMiddleware
is registered after app.UseAuthentication()
and app.UseAuthorization()
in the Configure
method to correctly log requests after authentication and authorization have been applied. This ensures that any telemetry data related to user identity or roles is captured, providing more accurate insights into authenticated requests.
Step 09: Visualize the Custom Event in Azure Insights
Once the response exception are logged as custom events, follow these steps to visualize them in Azure Insights:
Open Azure Portal:
Go to Azure Portal and navigate to your Application Insights resource.
Navigate to Logs (Analytics):
On the Application Insights overview page, select Logs (formerly known as Analytics).
Query Custom Events:
Use Kusto Query Language (KQL) to query the custom events. For example, to view the logged response exceptions:
Analyze the Data:
The query results will display logged response exceptions, stack traces, and any additional metadata (such as organization). Use the insights to analyze incoming data, diagnose issues, or optimize performance.Set Up Dashboards and Alerts:
You can save these queries as part of a dashboard or set up alerts based on specific conditions (e.g. exception, inner exception and stack trace) to proactively monitor important events.
To query response exceptions based on a specific database, use the following filter in your query: customDimensions.organization == 'YourDatabaseName'
. This will help you isolate and analyze logs related to the specified database, providing more targeted insights.
This step helps you to visualize and analyze the custom events captured by Azure Application Insights, providing deeper insight into your application’s behavior and performance.
Conclusion
In this guide, we've explored how to track response exceptions using custom event logging in Microsoft Azure Application Insights. By implementing this approach, you can capture and analyze exception data, providing valuable insights into errors and failures in your backend services. This method allows for detailed tracking of exceptions, user-related context, and application behavior, helping to diagnose issues and improve reliability. With these insights, you can enhance monitoring, streamline error handling, and ensure your application operates smoothly, enabling proactive improvements where necessary.