@Louis van Alphen and @Fin McCarthy one of our devs figured out how to get the logging scope to be honored again for .NET 8 Isolated functions. Finally found this documentation here calling out 2 new Nuget packages required as well as addition DI setup code to call.
https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide?tabs=windows#application-insights
Packages:
- Microsoft.Azure.Functions.Worker.ApplicationInsights
- Microsoft.ApplicationInsights.WorkerService
Then in the function's startup (Program.cs), under .ConfigureServices
you need to add the following:
services.AddApplicationInsightsTelemetryWorkerService(); services.ConfigureFunctionsApplicationInsights();
The biggest annoying part we have NOT resolved yet, is that it no longer honors the settings in our host.json
file (e.g. log levels and sampling setting).
The documentation calls out the fact that the Functions host and the isolated process worker have separate configs now and that host.json
settings will NOT affect logging from the worker (our code), so you have to deal with it programmatically.
In the case of disabling Sampling (which we wanted for some of our functions), you need to do it as part of the AddApplicationInsightsTelemetryWorkerService
call in one of its overloads, and then to go along with this (because we wanted Information logs to also be included) we need to remove the default logging rule, which is called out, but I'll include their code here in case the site changes
builder.ConfigureLogging(logging =>
{
// AppInsights uses a different filtering and only shows warnings or above by default. This removes that filter
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);
}
});
});
```The final thing (that I've run into so far), is that the `operation_Name` value is no longer being set for any of our worker logging, so this of course broke a bunch of our App Insights KQL queries that were looking for all the logs related to an individual function inside the function app.
Found [this github issue](https://github.com/Azure/azure-functions-dotnet-worker/issues/1540) where MS said to utilize a TelemetryProcessor to set the `operation_Name` with the `AzureFunctions_FunctionName` value so did that, and now things seem to be mostly back to normal.
We have found that the property names from the Logging scope (in the `customDimensions`) are no longer getting camel cased and are NOT getting prefixed with `prop__`, looks like the In-Process function handling was doing all that, so we're still having to update queries, but at least we have the logging scope items back.
Here's the telemetry processor code:
```csharp
// https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling?tabs=javascriptwebsdkloaderscript#create-a-telemetry-processor
public class FunctionTelemetryProcessor : ITelemetryProcessor
{
private const string _azureFunctionsFunctionNameFieldKey = "AzureFunctions_FunctionName";
private readonly ITelemetryProcessor _next;
public FunctionTelemetryProcessor(ITelemetryProcessor next)
{
_next = next;
}
public void Process(ITelemetry item)
{
if (item is ISupportProperties supportProperties)
{
if (string.IsNullOrWhiteSpace(item.Context.Operation.Name))
{
if (supportProperties.Properties.TryGetValue(_azureFunctionsFunctionNameFieldKey, out string functionName))
{
item.Context.Operation.Name = functionName;
}
}
}
_next.Process(item);
}
}
And add it using services.AddApplicationInsightsTelemetryProcessor<FunctionTelemetryProcessor>();