.Net
Install the package
Install the Azure Monitor Distro for .NET from NuGet:
Enabling Azure Monitor OpenTelemetry in your application
The following examples demonstrate how to integrate the Azure Monitor Distro into your application.
Example 1
To enable Azure Monitor Distro, add UseAzureMonitor()
to your Program.cs
file and set the APPLICATIONINSIGHTS_CONNECTION_STRING
environment variable to the connection string from your Application Insights resource.
Example 2
To enable Azure Monitor Distro with a hard-coded connection string, add UseAzureMonitor()
to your Program.cs
with the AzureMonitorOptions
containing the connection string.
Note that in the examples above, UseAzureMonitor
is added to the IServiceCollection
in the Program.cs
file. You can also add it in the ConfigureServices
method of your Startup.cs
file.
Note Multiple calls to
AddOpenTelemetry.UseAzureMonitor()
will NOT result in multiple providers. Only a singleTracerProvider
,MeterProvider
andLoggerProvider
will be created in the targetIServiceCollection
. To establish multiple providers use theSdk.CreateTracerProviderBuilder()
and/orSdk.CreateMeterProviderBuilder()
and/orLoggerFactory.CreateLogger
methods with the Azure Monitor Exporter instead of using Azure Monitor Distro.
Authenticate the client
Azure Active Directory (AAD) authentication is an optional feature that can be used with Azure Monitor Distro. To enable AAD authentication, set the Credential
property in AzureMonitorOptions
. This is made easy with the Azure Identity library, which provides support for authenticating Azure SDK clients with their corresponding Azure services.
With this configuration, the Azure Monitor Distro will use the credentials of the currently logged-in user or of the service principal to authenticate and send telemetry data to Azure Monitor.
Note that the Credential
property is optional. If it is not set, Azure Monitor Distro will use the Instrumentation Key from the Connection String to send data to Azure Monitor.
Advanced configuration
The Azure Monitor Distro includes .NET OpenTelemetry instrumentation for ASP.NET Core, HttpClient, and SQLClient. However, you can customize the instrumentation included or use additional instrumentation on your own using the OpenTelemetry API. Here are some examples of how to customize the instrumentation:
Customizing AspNetCoreInstrumentationOptions
Customizing HttpClientInstrumentationOptions
Customizing SqlClientInstrumentationOptions
Customizing Sampling Percentage
When using the Azure Monitor Distro, the sampling percentage for telemetry data is set to 100% (1.0F) by default. For example, let's say you want to set the sampling percentage to 50%. You can achieve this by modifying the code as follows:
Adding Custom ActivitySource to Traces
Adding Custom Meter to Metrics
Adding Additional Instrumentation
If you need to instrument a library or framework that isn't included in the Azure Monitor Distro, you can add additional instrumentation using the OpenTelemetry Instrumentation packages. For example, to add instrumentation for gRPC clients, you can add the OpenTelemetry.Instrumentation.GrpcNetClient package and use the following code:
Adding Another Exporter
Azure Monitor Distro uses the Azure Monitor exporter to send data to Application Insights. However, if you need to send data to other services, including Application Insights, you can add another exporter. For example, to add the Console exporter, you can install the OpenTelemetry.Exporter.Console package and use the following code:
Adding Custom Resource
To modify the resource, use the following code.
It is also possible to configure the Resource
by using following environmental variables:
Environment variable | Description |
---|---|
| Key-value pairs to be used as resource attributes. See the Resource SDK specification for more details. |
| Sets the value of the |
Example:
Last updated