如何在不修改 host.json 的情况下将调试日志从 Azure Function 应用直接发送到 Application Insights?

问题描述 投票:0回答:1

我正在隔离工作模型中使用 C#/.NET9 开发 Azure Function 应用程序。

根据 docs,应该可以将应用程序日志直接发送到 Application Insights,而不是通过 Function 主机中继它们。

appsettings.json
中,我有以下日志记录配置:

"Logging": {
  "LogLevel": {
    "Default": "Information",
    "AssetGovernance": "Debug"
  }
}

捕获

AssetGovernance*
类别中具有“调试”级别或更高级别的所有日志。

Program.cs
中,我将 Application Insights 配置为将应用程序日志直接发送到 Application Insights:

// Configure application
builder.ConfigureFunctionsWebApplication();

// Configure services
builder.Services.AddApplicationInsightsTelemetryWorkerService();
builder.Services.ConfigureFunctionsApplicationInsights();
builder.Services.Configure<WorkerOptions>(options => { });
...

// Configure logging
builder.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);
    }
});

现在的问题是,没有“调试”日志发送到 Application Insights,除非我在

logging.logLevel.Function: "Debug"
中设置
host.json
,这意味着日志实际上是通过 Function 主机中继的?

如何将日志直接发送到 Application Insights,以便日志过滤仅受

appsettings.json
控制(即
host.json
无效)?

c# .net azure-functions azure-functions-isolated
1个回答
0
投票

删除

defaultRule
后,过滤器默认为
Information
。 您将需要添加新的过滤规则,因为
host.json
中配置的日志级别不会影响工作进程。

此问题描述了如何使用所需的日志记录设置创建

appsettings.json
文件并将其加载为配置:


    builder.ConfigureAppConfiguration((hostContext, config) =>
    {
        // Add appsettings.json configuration so we can set logging in configuration.
        // Add in example a file called appsettings.json to the root and set the properties to:
        // Build Action: Content
        // Copy to Output Directory: Copy if newer
        //
        // Content:
        // {
        //   "Logging": {
        //     "LogLevel": {
        //       "Default": "Error" // Change this to ie Trace for more logging
        //     }
        //   }
        // }
        config.AddJsonFile("appsettings.json", optional: true);
    })
    .ConfigureLogging((hostingContext, logging) =>
    {
        // Make sure the configuration of the appsettings.json file is picked up.
        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
    });
© www.soinside.com 2019 - 2024. All rights reserved.