从哪里读取APPINSIGHTS_INSTRUMENTATIONKEY?

时间:2019-07-10 10:19:12

标签: azure azure-application-insights webjob

从哪里读取InstrumentationKey?

context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];

我已将该密钥放入本节的applicationInsights.config

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

但不是从那里拿来的...

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();

2 个答案:

答案 0 :(得分:0)

如果要在Azure上阅读它,只需在门户网站的Application Settings中进行设置。

如果是在本地运行,请在appsettings.json文件中添加APPINSIGHTS_INSTRUMENTATIONKEY字段。

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

更多信息,请参考此文档:Add Application Insights logging。希望这可以对您有所帮助。

答案 1 :(得分:-1)

您需要安装以下软件包:

  • Microsoft.Azure.WebJobs.Logging.ApplicationInsights(当前处于测试版)
  • Microsoft.Extensions.Logging
  • Microsoft.Extensions.Logging.Console

并按如下所示配置 JobHostConfiguration

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

您可以阅读有关Azure Web作业here的配置应用程序见解的信息。希望对您有所帮助。