我不想在 Program.cs 中键入应用程序洞察的 key ,我可以将其键入到某个配置文件或其他位置吗?这是一个 ASP .Net Core
我想在我的日志的应用程序洞察中包含一个寄存器,但进行了修改。现在我有一个寄存器,但我在 Program.cs 中键入 key ,当我更改环境时遇到“问题”。您知道在 Program.cs 上动态键入此键的方法吗?或者我可以在程序的其他位置执行此声明吗?
这是 Program.cs。它从 Main 开始,在启动 BuildWebHost 后,我在其中加载应用程序见解的 key ,这就是我想要更改的内容:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(logging =>
{
logging.AddApplicationInsights("db0fe38d-c208-8ed7-23e4ef4479bb");
// Optional: Apply filters to configure LogLevel Trace or above is sent to
// ApplicationInsights for all categories.
logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
// Additional filtering For category starting in "Microsoft",
// only Warning or above will be sent to Application Insights.
logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning);
}).Build();
我怎么说,我会避免在程序上键入 key ,并且我想从配置文件中获取此参数或在其他位置键入此声明
最佳答案
由于它是 ASP.NET Core 应用程序,因此您可以使用注入(inject) WebHostBuilderContext 的 ConfigureLogging
扩展方法来检索配置:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, logging) =>
{
var appInsightKey = hostingContext.Configuration["MyAppInsight"];
logging.AddApplicationInsights(appInsightKey);
// Optional: Apply filters to configure LogLevel Trace or above is sent to
// ApplicationInsights for all categories.
logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
// Additional filtering For category starting in "Microsoft",
// only Warning or above will be sent to Application Insights.
logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning);
}).Build();
关于c# - 我不想直接输入应用程序洞察的 key 来注册日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55352678/