我已经做了一些研究,日志级别信息应该足以确保日志显示在应用程序洞察中,但事实并非如此。
这篇文章似乎提供了答案,但仔细一看似乎并不是我的问题的解决方案:日志没有出现在带有 .NET 8 的 Azure Functions v4 的 Application Insights 中
这是host.json:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": false
},
"enableLiveMetricsFilters": true
},
"logLevel": {
"Function.MyFunction": "Information",
"default": "Information"
}
}
}
看起来这应该是一个简单的修复,尽管到目前为止我还无法找到正确的信息。
将提供的代码片段用于您的 program.cs 文件。此代码将您的函数应用配置为将遥测数据(包括日志)发送到 Application Insights。
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(s =>
{
s.AddApplicationInsightsTelemetryWorkerService();
s.ConfigureFunctionsApplicationInsights();
s.AddSingleton<IHttpResponderService, DefaultHttpResponderService>();
s.Configure<LoggerFilterOptions>(options =>
{
// 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://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-logs
LoggerFilterRule toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
== "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (toRemove is not null)
{
options.Rules.Remove(toRemove);
}
});
})
.Build();
如果您想捕获各个函数的特定日志级别,可以使用 host.json 文件。提供的代码片段显示了将名为“
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": false
},
"enableLiveMetricsFilters": true
},
"logLevel": {
"Function.<YOUR_FUNCTION_NAME>": "Information",
"default": "Information"
}
}
}