运行持久函数时,我在 Application Insights 日志流中收到大量详细和信息级别的日志记录。我尝试使用 host.json 和 Serilog 配置禁用这些,但没有任何运气。
我正在使用依赖注入,但没有任何特定于日志记录的内容(仅是自行创建的类)。
{
"version": "2.0",
"logging": {
"logLevel": {
"default": "Warning",
"DurableTask.AzureStorage": "Warning",
"DurableTask.Core": "Warning",
"Host.Triggers.DurableTask": "Warning",
"Function": "Warning",
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Exception"
}
}
}
}
似乎 host.json 被忽略,所有内容都被推送到日志流。
我还尝试使用以下代码添加 Serilog 来处理日志记录,但它具有相同的结果,详细日志记录仍然推送到 Application Insights
[assembly: FunctionsStartup(typeof(MultiHop.Functions.Startup))]
namespace MultiHop.Functions
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Warning()
.MinimumLevel.Override("Host.Triggers.DurableTask", LogEventLevel.Warning)
.MinimumLevel.Override("DurableTask.Core", LogEventLevel.Warning)
.MinimumLevel.Override("DurableTask.AzureStorage", LogEventLevel.Warning)
.MinimumLevel.Override("Function", LogEventLevel.Warning)
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.WriteTo.Console(LogEventLevel.Warning, theme: SystemConsoleTheme.Literate)
.CreateLogger();
builder.Services.AddLogging(configure => configure.AddSerilog(Log.Logger, true));
}
}
}
知道如何禁用这种详细信息记录吗?
为了减少本地运行 azure 函数时的详细日志记录,需要在
local.settings.json
文件中添加一些设置,例如日志级别属性下的 Azure WebJobs Host Middleware
、rpcWorkerProcess
到 None
。
"logging:logLevel:Microsoft.Azure.WebJobs.Script.WebHost.Middleware.SystemTraceMiddleware": "None",
"logging:logLevel:Worker.rpcWorkerProcess": "None"
根据您的上下文,我在 GitHub Azure Functions Core Tools Issues #1440 上发现了类似的问题讨论,其中作者解释了后端日志的来源 - 函数使用不同类型的主机,例如 .NET Host、Function运行时主机将充当父主机,子主机加载运行代码的功能,这些日志基于内部子主机的
host.json
文件配置出现在控制台中。
可以在此处找到禁用 AzureFunctions 日志但禁用您自己的日志记录的方法: https://github.com/anthonychu/functions-log-suppression
您必须禁用功能的日志记录(或设置为您想要的任何日志级别),然后为每个功能的用户日志记录添加日志级别。 显然没有办法为所有用户日志记录设置日志级别,因此您将遗憾地需要为每个功能执行此操作。
这是 URL 给出的示例,我已在本地运行它并且它对我有用。 在 local.stting.json 中:
{
"logging:logLevel:Microsoft": "None",
"logging:logLevel:Worker": "None",
"AzureFunctionsJobHost:logging:logLevel:default": "None",
"AzureFunctionsJobHost:logging:logLevel:Host.Function.Console": "Information",
"AzureFunctionsJobHost:logging:logLevel:Function.HttpTrigger1.User": "Information",
"AzureFunctionsJobHost:logging:logLevel:Function.HttpTrigger2.User": "Information"
}