-1

From where is the InstrumentationKey read?

context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];

I have put that key in the applicationInsights.config in the section

  <InstrumentationKey>94efb022-e651-46a0-b103-5735daa213f1</InstrumentationKey>

but its not taken from there...

var builder = new HostBuilder()
                .UseEnvironment("Development")
                .ConfigureWebJobs(b =>
                {
                    // Add extensions and other WebJobs services
                })
                .ConfigureAppConfiguration(b =>
                {
                    // Add configuration sources          
                })
                .ConfigureLogging((context, b) =>
                {
                    // Add Logging Providers
                    b.AddConsole();

                    // If this key exists in any config, use it to enable App Insights
                    string appInsightsKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
                    if (!string.IsNullOrEmpty(appInsightsKey))
                    {
                        // This uses the options callback to explicitly set the instrumentation key.
                        b.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
                    }
                })
                .UseConsoleLifetime();
HelloWorld
  • 4,671
  • 12
  • 46
  • 78

2 Answers2

0

If you want to read it on Azure just set it in the Application Settings in the Portal.

And if you are running it locally, in the appsettings.json file add APPINSIGHTS_INSTRUMENTATIONKEY field there.

{
    "AzureWebJobsStorage": "{storage connection string}",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "{instrumentation key}"
}

Further more information, refer to this doc :Add Application Insights logging. Hope this could help you.

George Chen
  • 13,703
  • 2
  • 11
  • 26
-1

You need to install below packages:

  • Microsoft.Azure.WebJobs.Logging.ApplicationInsights (Currently in beta)
  • Microsoft.Extensions.Logging
  • Microsoft.Extensions.Logging.Console

and Configure JobHostConfiguration as below:

string instrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
if (!string.IsNullOrEmpty(instrumentationKey))
{
      // build up a LoggerFactory with ApplicationInsights and a Console Logger
       config.LoggerFactory = new LoggerFactory().AddApplicationInsights(instrumentationKey, null).AddConsole();
       config.Tracing.ConsoleLevel = TraceLevel.Off;
}

You can read about configuration application insight with Azure web job here. Hope it helps.

Mohit Verma
  • 5,140
  • 2
  • 12
  • 27