I don't want to type the key of application insight in the Program.cs, Can I type it in some file of configurations or in another place? It's a ASP .Net Core
I want to include a register in application insights of my logs but with modifications. Now I have a register, but I am typing the key in the Program.cs and I have "a problem" when I change of environment. Do you know any way to type this key dynamicly on the Program.cs or can I do this declaration in another place of the program.
This is the Program.cs. It started with the Main and after it starts the BuildWebHost where I load the key of applications insights and it's what I want to change:
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();
How I said, I would avoid to type key on the Program and I would like take this param from a config file or type this declaration in another place
Just add UseApplicationInsights(), then remove the instrumentation key(assume the instrumentation key set in the appsettings.json).
Sample code as below, and works well at my side:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights() // add this line of code, and it will auto-read ikey from appsettings.json.
.UseStartup<Startup>()
.ConfigureLogging(logging =>
{
//then you can remove instrumentation key from here.
logging.AddApplicationInsights();
logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning);
}).Build();