C# ASP.NET Core 2.1 Web.API更改应用程序洞察用于日志记录的检测密钥

C# ASP.NET Core 2.1 Web.API更改应用程序洞察用于日志记录的检测密钥,c#,azure,asp.net-core-webapi,azure-application-insights,asp.net-core-2.1,C#,Azure,Asp.net Core Webapi,Azure Application Insights,Asp.net Core 2.1,我希望能够从WebApi(使用自定义记录器)记录到应用程序洞察。 一切正常,但我需要在appsetting.json中提供instrumentation key,并使用强制约定: "Values": { "AppInsightsKey": "I want to put key here" }, "ApplicationInsights": { "InstrumentationKey": "Now I must put key here" } 但是,我无法直接从Azure设置覆盖

我希望能够从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);
    }
}
使用Microsoft.Extensions.Logging;
...
公共类MyCustomLogger:IMyCustomLogger
{
专用只读ILogger\u记录器;
公共MyCustomLogger(ILogger记录器)
{
_记录器=记录器;
}
公共无效登录信息(字符串消息,参数对象[]args)
{
_logger.LogInformation(消息、参数);
}
}
另外,如果我可以覆盖Azure上的
ApplicationInsights.InstrumentationKey
,这也是合适的解决方案

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

请使用以下格式添加应用程序说明:InstrumentationKey作为Azure应用程序设置中的appsetting密钥。有关更多信息,请参阅此

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

请使用以下格式添加应用程序说明:InstrumentationKey作为Azure应用程序设置中的appsetting密钥。有关更多信息,请参阅此


在Program.cs中,您可以向WebHostBuilder添加洞察,而不是
.UseApplicationInsights()
,您可以在Program.cs中执行
.UseApplicationInsights(“YourKeyHere”)
操作,在Program.cs中,您可以向WebHostBuilder添加洞察,而不是
.UseApplicationInsights()
,您可以执行
.UseApplicationInsights(“YourKeyHere”)
将instrumentation键设置为环境变量“APPINSIGHTS\u INSTRUMENTATIONKEY”。它应该由Application Insights SDK拾取。

将instrumentation key设置为环境变量“APPINSIGHTS\u INSTRUMENTATIONKEY”。它应该由Application Insights SDK获取。

这会有问题,因此我希望将此密钥保留在appsettings.json中。这会有问题,因此我希望将此密钥保留在appsettings.json中。不幸的是,不会。日志组件未通过Azure中的此变量重写检测密钥。这仅在您希望在没有自定义日志记录的情况下连接“默认”应用程序洞察时有效。不幸的是,不能。日志组件未通过Azure中的此变量重写检测密钥。这仅在您希望在不使用自定义日志记录的情况下连接“默认”应用程序洞察时有效。