将我们的其中一个Azure Functions(v4)升级到.NET 8和隔离的worker后,我们遇到了一些日志记录问题; 某些(不是全部)错误级别日志仅出现在“App Insights 日志”的函数日志流中,而不出现在 Application Insights 跟踪日志中。
请注意,采样已(或应该)禁用,参考。 Azure 函数应用程序并不总是将日志发送到应用程序洞察。
在本地运行应用程序并将其连接到 Application Insights 时,查询跟踪时会显示所有日志。
程序.cs:
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
// Other service configurations not included
services.ConfigureFunctionsApplicationInsights();
services.AddApplicationInsightsTelemetryWorkerService();
})
.ConfigureLogging(logging =>
{
logging.Services.Configure<LoggerFilterOptions>(options =>
{
var defaultRule = options.Rules.FirstOrDefault(rule =>
rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (defaultRule is not null)
{
options.Rules.Remove(defaultRule);
}
});
})
.Build();
host.Run();
主机.json:
{
"version": "2.0",
"logging": {
"logLevel": {
"default": "Warning",
"Function": "Information",
"<project-namespace>": "Information"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": false
}
}
}
}
某些(不是全部)错误级别日志仅出现在“App Insights 日志”的函数日志流中,而不出现在 Application Insights 跟踪日志中。
通过使用 Http 触发功能和运行时堆栈 .NET 8.0 隔离获取所有类型的日志,包括应用程序洞察中的错误日志。检查以下代码和配置:
功能代码:
public class Function1
{
private readonly ILogger<Function1> _logger;
public Function1(ILogger<Function1> logger)
{
_logger = logger;
}
[Function("Function1")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{
_logger.LogTrace("this is Trace log");
_logger.LogDebug("this is Debug log");
_logger.LogWarning("this is Warning log");
_logger.LogError("this is Error log");
_logger.LogInformation("C# HTTP trigger function processed a request.");
return new OkObjectResult("Welcome to Azure Functions!");
}
}
.cs 项目:
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
程序.cs:
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.Configure<LoggerFilterOptions>(options =>
{
LoggerFilterRule toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
== "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (toRemove is not null)
{
options.Rules.Remove(toRemove);
}
});
})
.ConfigureAppConfiguration((hostContext, config) =>
{
config.AddJsonFile("host.json", optional: true);
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddApplicationInsights(console =>
{
console.IncludeScopes = true;
});
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
})
.Build();
host.Run();
主持人。 json:
{
"version": "2.0",
"logging": {
"logLevel": {
"default": "Trace"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
}
}
}
函数在本地运行成功。
输出: