在 Application Insights 中为 Worker Service 应用程序配置日志级别

Configure log level in Application Insights for Worker Service applications

有一个 NET5 控制台应用程序正在从警告及更高级别进行日志记录。这是后面的 documentation,但它无法记录信息类型。它确实记录警告。如何将日志级别更改为信息?

class Program
{
    static async Task Main(string[] args)
    {
        var services = new ServiceCollection();
        var startup = new Startup();

        startup.ConfigureServices(services);

        var serviceProvider = services.BuildServiceProvider();
        var executor = serviceProvider.GetService<IExecutor>();

        await executor.ExecuteTestsAsync();
    }
}

public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup()
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json");

        Configuration = builder.Build();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(Configuration);
        services.AddLogging();
        services.AddApplicationInsightsTelemetryWorkerService();
        services.AddSingleton<IExecutor, Executor>();
    }
}

    public Executor(ILogger<Executor> logger)
    {
        logger.LogInformation("Hello");//Not logged in AppInsights
        ...

appsettings.json

{
    "ApplicationInsights":
    {
        "InstrumentationKey": "putinstrumentationkeyhere"
    },
    "Logging":
    {
        "LogLevel":
        {
            "Default": "Information"
        }
    }
}

也试过这个:

services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Information));

您需要单独指定应用程序洞察的日志级别:

{
    "ApplicationInsights":
    {
        "InstrumentationKey": "putinstrumentationkeyhere"
    },
    "Logging":
    {
        "LogLevel":
        {
            "Default": "Information"
        },
        "ApplicationInsights": {
            "LogLevel": {
                "Default": "Information"
            }
        }
    }
}

(source)