使用
.net core 7
和 Webjob SDK
构建 WebJob,发现来自 Azure.Core
的打印太多,并尝试对其进行过滤,它可以使用代码,但配置失败,希望通过配置文件/环境配置日志记录变量
builder.ConfigureLogging((context, b) =>
{
b.AddConsole();
b.AddFilter("Azure.Core", LogLevel.None); // this work
});
从某些网络应用程序模板复制了以下应用程序设置,但它不起作用
"Logging": {
"LogLevel": {
"Default": "None",
"Azure.Core": "None"
}
},
尝试使用
AddConfiguration
但不确定我是否做对了。
builder.ConfigureLogging((context, b) =>
{
b.AddConfiguration(context.Configuration); // this line
b.AddConsole();
});
class Program
{
static async Task Main()
{
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorageQueues();
});
builder.ConfigureAppConfiguration((context, configurationBuilder) =>
{
configurationBuilder
.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
});
builder.ConfigureLogging((context, b) =>
{
b.AddConfiguration(context.Configuration);
b.AddConsole();
});
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
}
在您当前的配置中,
AddConfiguration
方法用于加载配置设置,但它可能不会直接应用于日志记录配置。
appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Azure.Core": "None"
}
}
}
程序.cs:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorageQueues();
});
builder.ConfigureAppConfiguration((context, configurationBuilder) =>
{
configurationBuilder
.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
});
builder.ConfigureLogging((context, b) =>
{
var loggingConfiguration = context.Configuration.GetSection("Logging");
b.AddConfiguration(loggingConfiguration);
b.AddConsole();
b.AddFilter("Azure.Core", LogLevel.None); // Apply the filter after loading the configuration
});
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
}
AddConfiguration
方法从appsettings.json文件中读取日志配置,随后的AddFilter
方法将“Azure.Core”命名空间的过滤器设置为LogLevel.None
。