Is there a way to automatically read the instrumentation key from the appsettings.json file?
Before, we could just do factory.AddApplicationInsights(app.ApplicationServices); in the configure method of startup.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@paulrennan Thanks for the question! We are investigating and will update you shortly.
If you also use Microsoft.ApplicationInsights.AspNetCore then appsettings.json will be checked for ikey, but other wise not supported at this point.
Tracking issue in ilogger repo:
https://github.com/Microsoft/ApplicationInsights-dotnet-logging/issues/272
@cijothomas Thanks for taking action on this!
@paulrennan Hope this addresses your question. We will now proceed to close this thread as resolved, but if there are any further questions regarding the documentation, please tag me in your reply and we will be happy to continue the conversation. Thanks!
How does this work, exactly? The instrumentation key can't be a constant; it has to come from the configuration file. In program.c I don't know if I'm sending logging information to my development AI or my production AI. I tried just removing the parameter from AddApplicationInsights, but that didn't work:
public static IWebHost BuildWebHost(string[] args) => WebHost
.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(
builder =>
{
builder.AddApplicationInsights();
})
.Build();
A code example and appsetting.json example would be very helpful.
@GammaFour
ConfigureLogging can get HostingContext. So the following would work.
From hosting context, you can figure out the environment as well..
hostingContext.HostingEnvironment.IsProduction()..
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, logging) =>
{
var appInsightKey = hostingContext.Configuration["myikeyfromconfig"];
logging.AddApplicationInsights(appInsightKey);
// Optional: Apply filters to configure LogLevel Trace or above is sent to
// ApplicationInsights for all categories.
logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
});
}
and example appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"myikeyfromconfig": "putrealikeyhere"
}
@GammaFour let us know if it helped. we'll probably update the doc with this snippet until actual code is modified to read ikey automatically from appsettings.json
Don't you have Build() that configuration before you can use it?
OK. That seems to work. As a side note, I needed to Build() the configuration before it would compile. And the AddFilter didn't seem to be necessary as it honored the values I had in my appsettings.json.
@GammaFour my full example should have worked... Not sure why you had to build explicitly. wont that be done by default from 2.0 onwards?
Yes filter was from another example, you can appl filtering from appsettings.json
https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#control-logging-level
Most helpful comment
@GammaFour
ConfigureLogging can get HostingContext. So the following would work.
From hosting context, you can figure out the environment as well..
hostingContext.HostingEnvironment.IsProduction()..and example appsettings.json