Reputation: 1926
I have several Azure Functions which I recently migrated from in-process model .NET 6 to isolated worker model .NET 8.
Everything seems to be working just fine, except I'm not seeing information logs in Azure when the Function is executed from Azure. When running the Azure Function locally and pointing to the same Application Insights instance, I can see in AppInsights the logs that are also printed in the console. But when the function executes from Azure itself (via timer trigger or manually via HTTP trigger), I cannot see the information level logging.
I've been following the thread and sample in https://github.com/Azure/azure-functions-dotnet-worker/issues/1182, but still couldn't make it work.
Program.cs:
var host = new HostBuilder()
.ConfigureAppConfiguration(
(hostContext, config) => { config.AddJsonFile("appsettings.json", optional: true); })
.ConfigureFunctionsWebApplication()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(
builder =>
{
// registering dependencies with Autofac
})
.ConfigureServices(
services =>
{
services.AddApplicationInsights();
// registering other dependencies
})
.ConfigureLogging(
(hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
})
.Build();
host.Run();
Extension method AddApplicationInsights
:
public static void AddApplicationInsights(this IServiceCollection serviceCollection)
{
serviceCollection.AddApplicationInsightsTelemetryWorkerService();
serviceCollection.ConfigureFunctionsApplicationInsights();
serviceCollection.Configure<LoggerFilterOptions>(
options =>
{
// See https://github.com/Azure/azure-functions-dotnet-worker/blob/main/samples/FunctionApp/Program.cs
// The Application Insights SDK adds a default logging filter that instructs ILogger to capture only Warning and more severe logs. Application Insights requires an explicit override.
// Log levels can also be configured using appsettings.json. For more information, see https://github.com/Azure/azure-functions-dotnet-worker/blob/main/samples/FunctionApp/Program.cs
var toRemove = options.Rules.FirstOrDefault(
rule => rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (toRemove is not null)
{
options.Rules.Remove(toRemove);
}
});
}
host.json:
{
"version": "2.0",
"functionTimeout": "23:00:00",
"logging": {
"logLevel": {
"default": "Information",
"Host.Results": "Information",
"Host.Aggregator": "Trace"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request;Trace"
}
}
}
}
appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
}
}
On function startup, I see the following being printed:
LoggerFilterOptions
{
"MinLevel": "None",
"Rules": [
{
"ProviderName": null,
"CategoryName": null,
"LogLevel": null,
"Filter": "<AddFilter>b__0"
},
{
"ProviderName": null,
"CategoryName": "Host.Results",
"LogLevel": "Information",
"Filter": null
},
{
"ProviderName": null,
"CategoryName": "Host.Aggregator",
"LogLevel": "Trace",
"Filter": null
},
{
"ProviderName": null,
"CategoryName": null,
"LogLevel": "Information",
"Filter": null
},
{
"ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
"CategoryName": null,
"LogLevel": "None",
"Filter": null
},
{
"ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
"CategoryName": null,
"LogLevel": null,
"Filter": "<AddFilter>b__0"
},
{
"ProviderName": "Microsoft.Azure.WebJobs.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider",
"CategoryName": null,
"LogLevel": "Trace",
"Filter": null
}
]
}
This is the logging before the migration:
Any hint what I might be missing here?
Upvotes: 0
Views: 1132
Reputation: 4738
It looks like you are removing the filter in the wrong place. You should do it in the ConfigureLogging
method:
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services => {
services.AddApplicationInsightsTelemetryWorkerService();
services.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);
}
});
})
.Build();
host.Run();
And not like you do:
.ConfigureServices(
services =>
{
serviceCollection.AddApplicationInsightsTelemetryWorkerService();
serviceCollection.ConfigureFunctionsApplicationInsights();
serviceCollection.Configure<LoggerFilterOptions>(
options =>
{
var toRemove = options.Rules.FirstOrDefault(
rule => rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (toRemove is not null)
{
options.Rules.Remove(toRemove);
}
});
})
Upvotes: 2