【问题标题】:ASP.NET Core 2.1 Web.API change application insights instrumentation key for loggingASP.NET Core 2.1 Web.API 更改应用程序洞察力记录的检测密钥
【发布时间】:2018-12-14 19:46:38
【问题描述】:

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

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

但是我不能直接从 Azure 设置覆盖第二个设置:

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

其实在我的Startup.cs我正在配置logger:

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

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

【问题讨论】:

    标签: c# azure asp.net-core-webapi azure-application-insights asp.net-core-2.1


    【解决方案1】:

    但是我不能直接从 Azure 设置覆盖第二个设置:

    请在 Azure 应用程序设置中添加具有以下格式的应用程序 ApplicationInsights:InstrumentationKey 作为 appsetting 键。更多信息请参考这个tutorial

    【讨论】:

      【解决方案2】:

      在您将洞察添加到 WebHostBuilder 的 Program.cs 中,您可以使用 .UseApplicationInsights("YourKeyHere") 而不是 .UseApplicationInsights("YourKeyHere")

      【讨论】:

      • 这会有问题,因此我想将此密钥保留在 appsettings.json 中。
      【解决方案3】:

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

      【讨论】:

      • 很遗憾没有。日志记录组件不会通过 Azure 中的此变量覆盖检测密钥。这仅适用于您想在没有自定义日志记录的情况下连接“默认”应用洞察。
      猜你喜欢
      • 2019-02-01
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      相关资源
      最近更新 更多