Fixing Application Insights Dependency Injection After Upgrading Microsoft.ApplicationInsights.WorkerService to 3.0.0
As part of our routine monthly maintenance on Azure Functions, we update NuGet packages to stay current and pick up bug fixes. This month that process led to a breaking change when we upgraded Microsoft.ApplicationInsights.WorkerService from version 2.23.0 to 3.0.0 on our .NET 8 Azure Functions.
After the upgrade, our Application Insights logging stopped working entirely. Telemetry was no longer being captured, and it became clear that the dependency injection configuration in our
The Problem
In version 2.23.0, we configured Application Insights in
This worked fine under 2.23.0. After upgrading to 3.0.0, however, dependency injection for Application Insights broke. The
The Fix
After reviewing the breaking changes introduced in version 3.0.0, we updated our
What Changed
There are three key differences between the old and new configuration:
1) Adaptive sampling is no longer configured on the service registration. In 2.23.0 we passed an options lambda to
2)
3) Sampling configuration moved into
Suggested Labels: Azure Functions, Microsoft Azure
If you run into this same issue after upgrading
After the upgrade, our Application Insights logging stopped working entirely. Telemetry was no longer being captured, and it became clear that the dependency injection configuration in our
Program.cs was the culprit. Here is what we had and how we fixed it.The Problem
In version 2.23.0, we configured Application Insights in
Program.cs like this:public static void Main(string[] args)
{
IHost host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(s => {
s.AddApplicationInsightsTelemetryWorkerService(options =>
{
options.EnableAdaptiveSampling = false;
});
s.ConfigureFunctionsApplicationInsights();
}).ConfigureLogging(logging =>
{
logging.Services.Configure<LoggerFilterOptions>(options =>
{
LoggerFilterRule? defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (defaultRule is not null)
{
options.Rules.Remove(defaultRule);
}
});
logging.AddApplicationInsights();
})
.Build();
host.Run();
}
This worked fine under 2.23.0. After upgrading to 3.0.0, however, dependency injection for Application Insights broke. The
EnableAdaptiveSampling option on AddApplicationInsightsTelemetryWorkerService no longer behaves the same way in version 3.0.0, and the logging.AddApplicationInsights() call in ConfigureLogging was no longer needed.The Fix
After reviewing the breaking changes introduced in version 3.0.0, we updated our
Program.cs to the following:public static void Main(string[] args)
{
IHost host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(s => {
s.AddApplicationInsightsTelemetryWorkerService();
s.ConfigureFunctionsApplicationInsights();
s.Configure<TelemetryConfiguration>(config =>
{
config.SamplingRatio = 1;
});
}).ConfigureLogging(logging =>
{
logging.Services.Configure<LoggerFilterOptions>(options =>
{
LoggerFilterRule? defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (defaultRule is not null)
{
options.Rules.Remove(defaultRule);
}
});
})
.Build();
host.Run();
}
What Changed
There are three key differences between the old and new configuration:
1) Adaptive sampling is no longer configured on the service registration. In 2.23.0 we passed an options lambda to
AddApplicationInsightsTelemetryWorkerService to disable adaptive sampling via EnableAdaptiveSampling = false. In 3.0.0 this option was removed from that registration. Sampling is now controlled directly on TelemetryConfiguration using the new SamplingRatio property. Setting SamplingRatio = 1 captures 100% of telemetry, which is the equivalent of disabling adaptive sampling.2)
logging.AddApplicationInsights() was removed. In 2.23.0 we explicitly added Application Insights to the logging pipeline inside ConfigureLogging. In 3.0.0 this is handled automatically by ConfigureFunctionsApplicationInsights(), making the explicit call redundant and in our case problematic.3) Sampling configuration moved into
ConfigureServices. The new pattern uses s.Configure<TelemetryConfiguration> inside ConfigureServices to set the sampling ratio, keeping all Application Insights setup in one place rather than split across ConfigureServices and ConfigureLogging.Suggested Labels: Azure Functions, Microsoft Azure
If you run into this same issue after upgrading
Microsoft.ApplicationInsights.WorkerService in your .NET 8 Azure Functions, hopefully this saves you some time tracking down the root cause.
Comments
Post a Comment