Search code examples
c#azureazure-webjobsiloggerappinsights

Ilogger not emitting to Application Insights in .NET Core 3.1 WebJob


I have an Azure Webjob developed with .NET Core 3.1.

We found that calls to ILogger.LogInformation("text message") seem to do nothing - as in, there are no AppInsights entries containing "text message" in the logs.

Not sure why this is - do we need to explicitly specify the log level (we currently don't) or is there an Azure setting we need to turn on? Is there anything else we can look at?

This is the bootstrap code in program.cs - nothing out of the ordinary:

   builder.ConfigureWebJobs((context, b) =>
   {
       b.AddTimers();
       b.AddAzureStorageCoreServices();
       b.AddAzureStorage();
   })
   .ConfigureLogging((context, b) =>
   {
       b.AddConfiguration(context.Configuration);
       b.AddSimpleConsole(c =>
   {
   c.TimestampFormat = "[yyyy-MM-dd HH:mm:ss UTC] ";
   c.UseUtcTimestamp = true;
   });
   var appInsightsKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
   if (!string.IsNullOrWhiteSpace(appInsightsKey))
   {
       b.AddApplicationInsightsWebJobs(o => { o.InstrumentationKey = appInsightsKey; });
   }

   b.AddAzureWebAppDiagnostics();
   });

The code that calls ILogger.LogInformation() is within a Mediator handler. Nothing exotic about it so won't include it here.

Here are the Nuget packages and version numbers:

Nuget packages referenced by Azure WebJob project


Solution

  • do we need to explicitly specify the log level (we currently don't)

    Yes you do. The default log level is warning. See the docs:

    The default setting for Application Insights is to only capture Warning and more severe logs.

    You can configure the default log level in the settings:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information"
        },
        "ApplicationInsights": {
          "LogLevel": {
            "Default": "Information"
          }
        }
      },
      "ApplicationInsights": {
        "ConnectionString": "InstrumentationKey=00000000-0000-0000-0000-000000000000"
      }
    }