This guide provides step-by-step instructions to migrate various .NET applications from using Application Insights software development kits (SDKs) to Azure Monitor OpenTelemetry.
Expect a similar experience with Azure Monitor OpenTelemetry instrumentation as with the Application Insights SDKs. For more information and a feature-by-feature comparison, see release state of features.
If you're getting started with Application Insights and don't need to migrate from the Classic API, see Enable Azure Monitor OpenTelemetry.
Prerequisites
- An ASP.NET Core web application already instrumented with Application Insights without any customizations
- An actively supported version of .NET
- An ASP.NET web application already instrumented with Application Insights
- An actively supported version of .NET Framework
- A Console application already instrumented with Application Insights
- An actively supported version of .NET Framework or .NET
- A WorkerService application already instrumented with Application Insights without any customizations
- An actively supported version of .NET
Tip
Our product group is actively seeking feedback on this documentation. Provide feedback to [email protected] or see the Support section.
Remove the Application Insights SDK
Note
Before continuing with these steps, you should confirm that you have a current backup of your application.
Remove NuGet packages
Remove the Microsoft.ApplicationInsights.AspNetCore
package from your csproj
.
dotnet remove package Microsoft.ApplicationInsights.AspNetCore
Remove Initialization Code and customizations
Remove any references to Application Insights types in your codebase.
Tip
After removing the Application Insights package, you can re-build your application to get a list of references that need to be removed.
Remove Application Insights from your ServiceCollection
by deleting the following line:
builder.Services.AddApplicationInsightsTelemetry();
Remove the ApplicationInsights
section from your appsettings.json
.
{
"ApplicationInsights": {
"ConnectionString": "<Your Connection String>"
}
}
Clean and Build
Inspect your bin directory to validate that all references to Microsoft.ApplicationInsights.*
were removed.
Test your application
Verify that your application has no unexpected consequences.
Remove NuGet packages
Remove the Microsoft.AspNet.TelemetryCorrelation
package and any Microsoft.ApplicationInsights.*
packages from your csproj
and packages.config
.
Delete the ApplicationInsights.config
file
Delete section from your application's Web.config
file
Two HttpModules were automatically added to your web.config when you first added ApplicationInsights to your project.
Any references to the TelemetryCorrelationHttpModule
and the ApplicationInsightsWebTracking
should be removed.
If you added Application Insights to your Internet Information Server (IIS) Modules, it should also be removed.
<configuration>
<system.web>
<httpModules>
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
<system.webServer>
<modules>
<remove name="TelemetryCorrelationHttpModule" />
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="managedHandler" />
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
</system.webServer>
</configuration>
Also review any assembly version redirections added to your web.config.
Remove Initialization Code and customizations
Remove any references to Application Insights types in your codebase.
Tip
After removing the Application Insights package, you can re-build your application to get a list of references that need to be removed.
- Remove references to
TelemetryConfiguration
or TelemetryClient
. It's a part of your application startup to initialize the Application Insights SDK.
The following scenarios are optional and apply to advanced users.
- If you have any more references to the
TelemetryClient
, which are used to manually record telemetry, they should be removed.
- If you added any custom filtering or enrichment in the form of a custom
TelemetryProcessor
or TelemetryInitializer
, they should be removed. You can find them referenced in your configuration.
- If your project has a
FilterConfig.cs
in the App_Start
directory, check for any custom exception handlers that reference Application Insights and remove.
Remove JavaScript Snippet
If you added the JavaScript SDK to collect client-side telemetry, it can also be removed although it continues to work without the .NET SDK.
For full code samples of what to remove, review the onboarding guide for the JavaScript SDK.
Remove any Visual Studio Artifacts
If you used Visual Studio to onboard to Application Insights, you could have more files left over in your project.
ConnectedService.json
might have a reference to your Application Insights resource.
[Your project's name].csproj
might have a reference to your Application Insights resource:
<ApplicationInsightsResourceId>/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourcegroups/Default-ApplicationInsights-EastUS/providers/microsoft.insights/components/WebApplication4</ApplicationInsightsResourceId>
Clean and Build
Inspect your bin directory to validate that all references to Microsoft.ApplicationInsights.
were removed.
Test your application
Verify that your application has no unexpected consequences.
Remove NuGet packages
Remove any Microsoft.ApplicationInsights.*
packages from your csproj
and packages.config
.
dotnet remove package Microsoft.ApplicationInsights
Remove Initialization Code and customizations
Remove any references to Application Insights types in your codebase.
Tip
After removing the Application Insights package, you can re-build your application to get a list of references that need to be removed.
Remove references to TelemetryConfiguration
or TelemetryClient
. It should be part of your application startup to initialize the Application Insights SDK.
var config = TelemetryConfiguration.CreateDefault();
var client = new TelemetryClient(config);
Tip
If you've used AddApplicationInsightsTelemetryWorkerService()
to add Application Insights to your ServiceCollection
, refer to the WorkerService tabs.
Clean and Build
Inspect your bin directory to validate that all references to Microsoft.ApplicationInsights.
were removed.
Test your application
Verify that your application has no unexpected consequences.
Remove NuGet packages
Remove the Microsoft.ApplicationInsights.WorkerService
package from your csproj
.
dotnet remove package Microsoft.ApplicationInsights.WorkerService
Remove Initialization Code and customizations
Remove any references to Application Insights types in your codebase.
Tip
After removing the Application Insights package, you can re-build your application to get a list of references that need to be removed.
Remove Application Insights from your ServiceCollection
by deleting the following line:
builder.Services.AddApplicationInsightsTelemetryWorkerService();
Remove the ApplicationInsights
section from your appsettings.json
.
{
"ApplicationInsights": {
"ConnectionString": "<Your Connection String>"
}
}
Clean and Build
Inspect your bin directory to validate that all references to Microsoft.ApplicationInsights.*
were removed.
Test your application
Verify that your application has no unexpected consequences.
Tip
Our product group is actively seeking feedback on this documentation. Provide feedback to [email protected] or see the Support section.
Enable OpenTelemetry
We recommended creating a development resource and using its connection string when following these instructions.
Plan to update the connection string to send telemetry to the original resource after confirming migration is successful.
Install the Azure Monitor Distro
Our Azure Monitor Distro enables automatic telemetry by including OpenTelemetry instrumentation libraries for collecting traces, metrics, logs, and exceptions, and allows collecting custom telemetry.
Installing the Azure Monitor Distro brings the OpenTelemetry SDK as a dependency.
dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore
Add and configure both OpenTelemetry and Azure Monitor
The OpenTelemery SDK must be configured at application startup as part of your ServiceCollection
, typically in the Program.cs
.
OpenTelemetry has a concept of three signals; Traces, Metrics, and Logs.
The Azure Monitor Distro configures each of these signals.
Program.cs
The following code sample demonstrates the basics.
using Azure.Monitor.OpenTelemetry.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Call AddOpenTelemetry() to add OpenTelemetry to your ServiceCollection.
// Call UseAzureMonitor() to fully configure OpenTelemetry.
builder.Services.AddOpenTelemetry().UseAzureMonitor();
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
}
}
We recommend setting your Connection String in an environment variable:
APPLICATIONINSIGHTS_CONNECTION_STRING=<Your Connection String>
More options to configure the Connection String are detailed here: Configure the Application Insights Connection String.
Install the OpenTelemetry SDK via Azure Monitor
Installing the Azure Monitor Exporter brings the OpenTelemetry SDK as a dependency.
dotnet add package Azure.Monitor.OpenTelemetry.Exporter
Configure OpenTelemetry as part of your application startup
The OpenTelemery SDK must be configured at application startup, typically in the Global.asax.cs
.
OpenTelemetry has a concept of three signals; Traces, Metrics, and Logs.
Each of these signals needs to be configured as part of your application startup.
TracerProvider
, MeterProvider
, and ILoggerFactory
should be created once for your application and disposed when your application shuts down.
Global.asax.cs
The following code sample shows a simple example meant only to show the basics.
No telemetry is collected at this point.
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
public class Global : System.Web.HttpApplication
{
private TracerProvider? tracerProvider;
private MeterProvider? meterProvider;
// The LoggerFactory needs to be accessible from the rest of your application.
internal static ILoggerFactory loggerFactory;
protected void Application_Start()
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.Build();
this.meterProvider = Sdk.CreateMeterProviderBuilder()
.Build();
loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry();
});
}
protected void Application_End()
{
this.tracerProvider?.Dispose();
this.meterProvider?.Dispose();
loggerFactory?.Dispose();
}
}
Install the OpenTelemetry SDK via Azure Monitor
Installing the Azure Monitor Exporter brings the OpenTelemetry SDK as a dependency.
dotnet add package Azure.Monitor.OpenTelemetry.Exporter
Configure OpenTelemetry as part of your application startup
The OpenTelemery SDK must be configured at application startup, typically in the Program.cs
.
OpenTelemetry has a concept of three signals; Traces, Metrics, and Logs.
Each of these signals needs to be configured as part of your application startup.
TracerProvider
, MeterProvider
, and ILoggerFactory
should be created once for your application and disposed when your application shuts down.
The following code sample shows a simple example meant only to show the basics.
No telemetry is collected at this point.
Program.cs
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
internal class Program
{
static void Main(string[] args)
{
TracerProvider tracerProvider = Sdk.CreateTracerProviderBuilder()
.Build();
MeterProvider meterProvider = Sdk.CreateMeterProviderBuilder()
.Build();
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry();
});
Console.WriteLine("Hello, World!");
// Dispose tracer provider before the application ends.
// It will flush the remaining spans and shutdown the tracing pipeline.
tracerProvider.Dispose();
// Dispose meter provider before the application ends.
// It will flush the remaining metrics and shutdown the metrics pipeline.
meterProvider.Dispose();
// Dispose logger factory before the application ends.
// It will flush the remaining logs and shutdown the logging pipeline.
loggerFactory.Dispose();
}
}
Install the OpenTelemetry SDK via Azure Monitor
Installing the Azure Monitor Exporter brings the OpenTelemetry SDK as a dependency.
dotnet add package Azure.Monitor.OpenTelemetry.Exporter
You must also install the OpenTelemetry Extensions Hosting package.
dotnet add package OpenTelemetry.Extensions.Hosting
Configure OpenTelemetry as part of your application startup
The OpenTelemery SDK must be configured at application startup, typically in the Program.cs
.
OpenTelemetry has a concept of three signals; Traces, Metrics, and Logs.
Each of these signals needs to be configured as part of your application startup.
TracerProvider
, MeterProvider
, and ILoggerFactory
should be created once for your application and disposed when your application shuts down.
The following code sample shows a simple example meant only to show the basics.
No telemetry is collected at this point.
Program.cs
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
public class Program
{
public static void Main(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
builder.Services.AddOpenTelemetry()
.WithTracing()
.WithMetrics();
builder.Logging.AddOpenTelemetry();
var host = builder.Build();
host.Run();
}
}
Tip
Our product group is actively seeking feedback on this documentation. Provide feedback to [email protected] or see the Support section.
Instrumentation libraries can be added to your project to auto collect telemetry about specific components or dependencies.
The following libraries are included in the Distro.
Customizing instrumentation libraries
The Azure Monitor Distro includes .NET OpenTelemetry instrumentation for ASP.NET Core, HttpClient, and SQLClient.
You can customize these included instrumentations or manually add extra instrumentation on your own using the OpenTelemetry API.
Here are some examples of how to customize the instrumentation:
Customizing AspNetCoreTraceInstrumentationOptions
builder.Services.AddOpenTelemetry().UseAzureMonitor();
builder.Services.Configure<AspNetCoreTraceInstrumentationOptions>(options =>
{
options.RecordException = true;
options.Filter = (httpContext) =>
{
// only collect telemetry about HTTP GET requests
return HttpMethods.IsGet(httpContext.Request.Method);
};
});
Customizing HttpClientTraceInstrumentationOptions
builder.Services.AddOpenTelemetry().UseAzureMonitor();
builder.Services.Configure<HttpClientTraceInstrumentationOptions>(options =>
{
options.RecordException = true;
options.FilterHttpRequestMessage = (httpRequestMessage) =>
{
// only collect telemetry about HTTP GET requests
return HttpMethods.IsGet(httpRequestMessage.Method.Method);
};
});
Customizing SqlClientInstrumentationOptions
We provide the SQLClient instrumentation within our package while it's still in beta. When it reaches a stable release, we'll include it as a standard package reference. Until then, to customize the SQLClient instrumentation, add the OpenTelemetry.Instrumentation.SqlClient
package reference to your project and use its public API.
dotnet add package --prerelease OpenTelemetry.Instrumentation.SqlClient
builder.Services.AddOpenTelemetry().UseAzureMonitor().WithTracing(builder =>
{
builder.AddSqlClientInstrumentation(options =>
{
options.SetDbStatementForStoredProcedure = false;
});
});
Instrumentation libraries can be added to your project to auto collect telemetry about specific components or dependencies. We recommend the following libraries:
OpenTelemetry.Instrumentation.AspNet can be used to collect telemetry for incoming requests. Azure Monitor maps it to Request Telemetry.
dotnet add package OpenTelemetry.Instrumentation.AspNet
It requires adding an extra HttpModule to your Web.config
:
<system.webServer>
<modules>
<add
name="TelemetryHttpModule"
type="OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule,
OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule"
preCondition="integratedMode,managedHandler" />
</modules>
</system.webServer>
A complete getting started guide is available here: OpenTelemetry.Instrumentation.AspNet Readme
OpenTelemetry.Instrumentation.Http can be used to collect telemetry for outbound http dependencies. Azure Monitor maps it to Dependency Telemetry.
dotnet add package OpenTelemetry.Instrumentation.Http
A complete getting started guide is available here: OpenTelemetry.Instrumentation.Http Readme
OpenTelemetry.Instrumentation.SqlClient can be used to collect telemetry for MS SQL dependencies. Azure Monitor maps it to Dependency Telemetry.
dotnet add package --prerelease OpenTelemetry.Instrumentation.SqlClient
A complete getting started guide is available here: OpenTelemetry.Instrumentation.SqlClient Readme
Global.asax.cs
The following code sample expands on the previous example.
It now collects telemetry, but doesn't yet send to Application Insights.
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
public class Global : System.Web.HttpApplication
{
private TracerProvider? tracerProvider;
private MeterProvider? meterProvider;
internal static ILoggerFactory loggerFactory;
protected void Application_Start()
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation()
.AddHttpClientInstrumentation()
.AddSqlClientInstrumentation()
.Build();
this.meterProvider = Sdk.CreateMeterProviderBuilder()
.AddAspNetInstrumentation()
.AddHttpClientInstrumentation()
.Build();
loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry();
});
}
protected void Application_End()
{
this.tracerProvider?.Dispose();
this.meterProvider?.Dispose();
loggerFactory?.Dispose();
}
}
Instrumentation libraries can be added to your project to auto collect telemetry about specific components or dependencies. We recommend the following libraries:
OpenTelemetry.Instrumentation.Http can be used to collect telemetry for outbound http dependencies. Azure Monitor maps it to Dependency Telemetry.
dotnet add package OpenTelemetry.Instrumentation.Http
A complete getting started guide is available here: OpenTelemetry.Instrumentation.Http Readme
OpenTelemetry.Instrumentation.SqlClient can be used to collect telemetry for MS SQL dependencies. Azure Monitor maps it to Dependency Telemetry.
dotnet add package --prerelease OpenTelemetry.Instrumentation.SqlClient
A complete getting started guide is available here: OpenTelemetry.Instrumentation.SqlClient Readme
The following code sample expands on the previous example.
It now collects telemetry, but doesn't yet send to Application Insights.
Program.cs
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
internal class Program
{
static void Main(string[] args)
{
TracerProvider tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddSqlClientInstrumentation()
.Build();
MeterProvider meterProvider = Sdk.CreateMeterProviderBuilder()
.AddHttpClientInstrumentation()
.Build();
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry();
});
Console.WriteLine("Hello, World!");
tracerProvider.Dispose();
meterProvider.Dispose();
loggerFactory.Dispose();
}
}
Instrumentation libraries can be added to your project to auto collect telemetry about specific components or dependencies. We recommend the following libraries:
OpenTelemetry.Instrumentation.Http can be used to collect telemetry for outbound http dependencies. Azure Monitor maps it to Dependency Telemetry.
dotnet add package OpenTelemetry.Instrumentation.Http
A complete getting started guide is available here: OpenTelemetry.Instrumentation.Http Readme
OpenTelemetry.Instrumentation.SqlClient can be used to collect telemetry for MS SQL dependencies. Azure Monitor maps it to Dependency Telemetry.
dotnet add package --prerelease OpenTelemetry.Instrumentation.SqlClient
A complete getting started guide is available here: OpenTelemetry.Instrumentation.SqlClient Readme
The following code sample expands on the previous example.
It now collects telemetry, but doesn't yet send to Application Insights.
Program.cs
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
public class Program
{
public static void Main(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
builder.Services.AddOpenTelemetry()
.WithTracing(builder =>
{
builder.AddHttpClientInstrumentation();
builder.AddSqlClientInstrumentation();
})
.WithMetrics(builder =>
{
builder.AddHttpClientInstrumentation();
});
builder.Logging.AddOpenTelemetry();
var host = builder.Build();
host.Run();
}
}
Application Insights offered many more configuration options via ApplicationInsightsServiceOptions
.
Application Insights Setting |
OpenTelemetry Alternative |
AddAutoCollectedMetricExtractor |
N/A |
ApplicationVersion |
Set "service.version" on Resource |
ConnectionString |
See instructions on configuring the Connection String. |
DependencyCollectionOptions |
N/A. To customize dependencies, review the available configuration options for applicable Instrumentation libraries. |
DeveloperMode |
N/A |
EnableActiveTelemetryConfigurationSetup |
N/A |
EnableAdaptiveSampling |
N/A. Only fixed-rate sampling is supported. |
EnableAppServicesHeartbeatTelemetryModule |
N/A |
EnableAuthenticationTrackingJavaScript |
N/A |
EnableAzureInstanceMetadataTelemetryModule |
N/A |
EnableDependencyTrackingTelemetryModule |
See instructions on filtering Traces. |
EnableDiagnosticsTelemetryModule |
N/A |
EnableEventCounterCollectionModule |
N/A |
EnableHeartbeat |
N/A |
EnablePerformanceCounterCollectionModule |
N/A |
EnableQuickPulseMetricStream |
AzureMonitorOptions.EnableLiveMetrics |
EnableRequestTrackingTelemetryModule |
See instructions on filtering Traces. |
EndpointAddress |
Use ConnectionString. |
InstrumentationKey |
Use ConnectionString. |
RequestCollectionOptions |
N/A. See OpenTelemetry.Instrumentation.AspNetCore options. |
Remove custom configurations
The following scenarios are optional and only apply to advanced users.
If you have any more references to the TelemetryClient
, which could be used to manually record telemetry, they should be removed.
If you added any custom filtering or enrichment in the form of a custom TelemetryProcessor
or TelemetryInitializer
, they should be removed. They can be found in your ServiceCollection
.
builder.Services.AddSingleton<ITelemetryInitializer, MyCustomTelemetryInitializer>();
builder.Services.AddApplicationInsightsTelemetryProcessor<MyCustomTelemetryProcessor>();
Remove JavaScript Snippet
If you used the Snippet provided by the Application Insights .NET SDK, it must also be removed.
For full code samples of what to remove, review the guide enable client-side telemetry for web applications.
If you added the JavaScript SDK to collect client-side telemetry, it can also be removed although it continues to work without the .NET SDK.
For full code samples of what to remove, review the onboarding guide for the JavaScript SDK.
Remove any Visual Studio Artifacts
If you used Visual Studio to onboard to Application Insights, you could have more files left over in your project.
Properties/ServiceDependencies
directory might have a reference to your Application Insights resource.
To send your telemetry to Application Insights, the Azure Monitor Exporter must be added to the configuration of all three signals.
Global.asax.cs
The following code sample expands on the previous example.
It now collects telemetry and sends to Application Insights.
public class Global : System.Web.HttpApplication
{
private TracerProvider? tracerProvider;
private MeterProvider? meterProvider;
internal static ILoggerFactory loggerFactory;
protected void Application_Start()
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation()
.AddHttpClientInstrumentation()
.AddSqlClientInstrumentation()
.AddAzureMonitorTraceExporter()
.Build();
this.meterProvider = Sdk.CreateMeterProviderBuilder()
.AddAspNetInstrumentation()
.AddHttpClientInstrumentation()
.AddAzureMonitorMetricExporter()
.Build();
loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(logging => logging.AddAzureMonitorLogExporter());
});
}
protected void Application_End()
{
this.tracerProvider?.Dispose();
this.meterProvider?.Dispose();
loggerFactory?.Dispose();
}
}
We recommend setting your Connection String in an environment variable:
APPLICATIONINSIGHTS_CONNECTION_STRING=<Your Connection String>
More options to configure the Connection String are detailed here: Configure the Application Insights Connection String.
To send your telemetry to Application Insights, the Azure Monitor Exporter must be added to the configuration of all three signals.
Program.cs
The following code sample expands on the previous example.
It now collects telemetry and sends to Application Insights.
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
internal class Program
{
static void Main(string[] args)
{
TracerProvider tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddSqlClientInstrumentation()
.AddAzureMonitorTraceExporter()
.Build();
MeterProvider meterProvider = Sdk.CreateMeterProviderBuilder()
.AddHttpClientInstrumentation()
.AddAzureMonitorMetricExporter()
.Build();
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(logging => logging.AddAzureMonitorLogExporter());
});
Console.WriteLine("Hello, World!");
tracerProvider.Dispose();
meterProvider.Dispose();
loggerFactory.Dispose();
}
}
We recommend setting your Connection String in an environment variable:
APPLICATIONINSIGHTS_CONNECTION_STRING=<Your Connection String>
More options to configure the Connection String are detailed here: Configure the Application Insights Connection String.
Remove custom configurations
The following scenarios are optional and apply to advanced users.
If you have any more references to the TelemetryClient
, which is used to manually record telemetry, they should be removed.
Remove any custom filtering or enrichment added as a custom TelemetryProcessor
or TelemetryInitializer
. The configuration references them.
Remove any Visual Studio Artifacts
If you used Visual Studio to onboard to Application Insights, you could have more files left over in your project.
ConnectedService.json
might have a reference to your Application Insights resource.
[Your project's name].csproj
might have a reference to your Application Insights resource:
<ApplicationInsightsResourceId>/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourcegroups/Default-ApplicationInsights-EastUS/providers/microsoft.insights/components/WebApplication4</ApplicationInsightsResourceId>
To send your telemetry to Application Insights, the Azure Monitor Exporter must be added to the configuration of all three signals.
Program.cs
The following code sample expands on the previous example.
It now collects telemetry and sends to Application Insights.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
public class Program
{
public static void Main(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
builder.Services.AddOpenTelemetry()
.WithTracing(builder =>
{
builder.AddHttpClientInstrumentation();
builder.AddSqlClientInstrumentation();
builder.AddAzureMonitorTraceExporter();
})
.WithMetrics(builder =>
{
builder.AddHttpClientInstrumentation();
builder.AddAzureMonitorMetricExporter();
});
builder.Logging.AddOpenTelemetry(logging => logging.AddAzureMonitorLogExporter());
var host = builder.Build();
host.Run();
}
}
We recommend setting your Connection String in an environment variable:
APPLICATIONINSIGHTS_CONNECTION_STRING=<Your Connection String>
More options to configure the Connection String are detailed here: Configure the Application Insights Connection String.
More configurations
Application Insights offered many more configuration options via ApplicationInsightsServiceOptions
.
Application Insights Setting |
OpenTelemetry Alternative |
AddAutoCollectedMetricExtractor |
N/A |
ApplicationVersion |
Set "service.version" on Resource |
ConnectionString |
See instructions on configuring the Connection String. |
DependencyCollectionOptions |
N/A. To customize dependencies, review the available configuration options for applicable Instrumentation libraries. |
DeveloperMode |
N/A |
EnableAdaptiveSampling |
N/A. Only fixed-rate sampling is supported. |
EnableAppServicesHeartbeatTelemetryModule |
N/A |
EnableAzureInstanceMetadataTelemetryModule |
N/A |
EnableDependencyTrackingTelemetryModule |
See instructions on filtering Traces. |
EnableDiagnosticsTelemetryModule |
N/A |
EnableEventCounterCollectionModule |
N/A |
EnableHeartbeat |
N/A |
EnablePerformanceCounterCollectionModule |
N/A |
EnableQuickPulseMetricStream |
AzureMonitorOptions.EnableLiveMetrics |
EndpointAddress |
Use ConnectionString. |
InstrumentationKey |
Use ConnectionString. |
Remove Custom Configurations
The following scenarios are optional and apply to advanced users.
If you have any more references to the TelemetryClient
, which are used to manually record telemetry, they should be removed.
If you added any custom filtering or enrichment in the form of a custom TelemetryProcessor
or TelemetryInitializer
, it should be removed. You can find references in your ServiceCollection
.
builder.Services.AddSingleton<ITelemetryInitializer, MyCustomTelemetryInitializer>();
builder.Services.AddApplicationInsightsTelemetryProcessor<MyCustomTelemetryProcessor>();
Remove any Visual Studio Artifacts
If you used Visual Studio to onboard to Application Insights, you could have more files left over in your project.
Properties/ServiceDependencies
directory might have a reference to your Application Insights resource.
Tip
Our product group is actively seeking feedback on this documentation. Provide feedback to [email protected] or see the Support section.
Next steps
Tip
Our product group is actively seeking feedback on this documentation. Provide feedback to [email protected] or see the Support section.
Support