[英]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"
}
有什么辦法可以正確設置它?
實際上,在我的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
,那么這也是正確的解決方案。
但是,我不能直接從Azure設置覆蓋此第二個設置:
請添加具有以下格式ApplicationInsights:InstrumentationKey的應用程序作為Azure應用程序設置中的appsetting密鑰。 有關更多信息,請參閱本教程 。
將檢測鍵設置為環境變量“ APPINSIGHTS_INSTRUMENTATIONKEY”。 它應該由Application Insights SDK提取。
在將Insights添加到WebHostBuilder的Program.cs中,可以執行.UseApplicationInsights("YourKeyHere")
而不是.UseApplicationInsights()
.UseApplicationInsights("YourKeyHere")
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.