Justin
Justin

Reputation: 3

Configure log level in Application Insights for Worker Service applications

There is a NET5 console app that is logging from warning and above. This is the documentation followed but it is not working to log information types. It does log warnings. How to change the log level to information?

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"
        }
    }
}

Also tried this:

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

Upvotes: 0

Views: 1204

Answers (1)

Peter Bons
Peter Bons

Reputation: 29840

You need to specify the log level for application insights seperately:

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

(source)

Upvotes: 2

Related Questions