我正在 .Net C# Azure 函数应用程序中配置日志记录,以将日志消息发送到 Azure Application Insights。请参阅下面我的 Program.cs 内容。如何配置日志记录以不记录 EntityFramework 消息?
请注意,我要删除的规则应该阻止记录除错误之外的任何内容,并且我确实希望记录 LogInfo() 消息。
var config = new ConfigurationBuilder()
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var connectionString = config["defaultConnection"];
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.ConfigureLogging(logging =>
{
logging.Services.Configure<LoggerFilterOptions>(options =>
{
foreach (LoggerFilterRule rule in options.Rules)
{
if (rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider")
defaultRule = rule;
}
if (defaultRule is not null)
{
options.Rules.Remove(defaultRule);
}
});
})
.ConfigureOpenApi()
.ConfigureServices(services =>
{
services.AddDbContext<DijestContext>(options =>
options.UseSqlServer(
connectionString,
sqlServerOptions => sqlServerOptions.CommandTimeout(600)));
}
)
.Build();
host.Run();
我认为将其添加到您的
ConfigureLogging
的开头应该可以解决问题
logging.AddFilter("Microsoft.EntityFrameworkCore", LogLevel.None);
logging.AddFilter("Default", LogLevel.Information);