首页 > 解决方案 > ASP.NET Core 2.1 Web.API 更改应用程序洞察力记录的检测密钥

问题描述

我想启用从 WebApi (使用自定义记录器)记录到应用程序洞察力。一切正常,但我需要提供instrumentation key强制appsetting.json约定:

"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。如果我可以ApplicationInsights.InstrumentationKey在 Azure 上覆盖,这也是正确的解决方案。

标签: c#azureasp.net-core-webapiazure-application-insightsasp.net-core-2.1

解决方案


将检测密钥设置为环境变量“APPINSIGHTS_INSTRUMENTATIONKEY”。它应该由 Application Insights SDK 获取。


推荐阅读