Search code examples
azureazure-application-insightsazure-webjobsazure-webjobssdkappinsights

Application Insights Visual Studio integration not working for Console application (WebJob)


I have a console application we deploy as a WebJob in Azure. Application Insights is working, because the telemetry is send to Azure correctly. But the "Application Insights Search" window in Visual Studio (2019) is not picking up the telemetry locally. What do I need to configure, so the application insights window will show the telemetry for the console application?

In the solution we have an MVC website and the WebJob together in the same solution. The telemetry works locally for the MVC website project, but not for the webjob project.

I have integrated Application Insights with the following code:

builder.ConfigureLogging((context, loggingBuilder) =>
{
  loggingBuilder.AddApplicationInsightsWebJobs(o =>
  {
    o.InstrumentationKey = instrumentationKey;
    o.EnableLiveMetrics = true;
    o.LiveMetricsInitializationDelay = TimeSpan.FromMinutes(1);
    o.SamplingExcludedTypes = "Exception";
  });
});

The instrumentation key comes from the appsettings.json:

  "Company.Logging": {
    "InstrumentationKey": "1242189c-1192-41d9-b877-b26890bd0eef",
    "LoggingServiceUrl": "https://loggingapidev.company.com/api/logs/batch"
  }

We use the same configuration for the website, that works.

I did try to add the "APPINSIGHTS_INSTRUMENTATIONKEY" key in the appsettings.json and as an environment variable. That didn't have any effect.

Does anyone know what I can do to make this work? It would make it more intuitive for developers to work with application insights. Now I have to direct them to the portal where they can find their local telemetry.


Solution

  • I tested it with console project(webjob v3), and it works fine. I can see the logs are shown in Visual Studio Application Insights Search window.

    If this issue still occurs at your side, please give it a try by manually adding an empty ApplicationInsights.config file to your webjob project.

    Workflow:

    • Right click project: Add -> new item...
    • Choose: Application Configuration File, like "JavaScript JSON Configuration File".
    • Name: ApplicationInsights.config
    • Remove the content of this file -> then click save. Then right right this file-> select Properties -> Set "Copy to Output Directory" as "Copy if newer"