ASP.NET Core 2.1 Web.API 更改用于日志记录的 Application Insights Instrumentation 密钥

ASP.NET Core 2.1 Web.API change application insights instrumentation key for logging

我想启用从 WebApi(使用自定义记录器)记录应用见解。 一切正常,但我需要在 appsetting.json 中提供强制约定的 instrumentation key

"Values": {
   "AppInsightsKey":  "I want to put key here" 
},
"ApplicationInsights": {
   "InstrumentationKey": "Now I must put key here"
}

但是我无法直接从 Azure 设置覆盖第二个设置:

有什么方法可以正确设置吗?

实际上在我的 Startup.cs 我正在配置记录器:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
}

我的记录器包装器:

using Microsoft.Extensions.Logging;
...
public class MyCustomLogger : IMyCustomLogger
{
    private readonly ILogger _logger;

    public MyCustomLogger(ILogger<MyCustomLogger> logger)
    {
        _logger = logger;
    }
    public void LogInformation(string message, params object[] args)
    {
        _logger.LogInformation(message, args);
    }
}

PS。如果我可以在 Azure 上覆盖 ApplicationInsights.InstrumentationKey,这也是正确的解决方案。

However I can't override this second setting directly from Azure Settings:

请在 Azure 应用程序设置中添加格式为 ApplicationInsights:InstrumentationKey 的应用程序作为 appsetting 键。更多信息请参考此tutorial.

在 Program.cs 中,您可以将 Insights 添加到 WebHostBuilder,而不是 .UseApplicationInsights(),您可以 .UseApplicationInsights("YourKeyHere").

将检测密钥设置为环境变量 "APPINSIGHTS_INSTRUMENTATIONKEY"。它应该被 Application Insights SDK 拾取。