I am following this article: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1
I am trying to only log my own custom logging in a asp.net core 3.1 API. And not all logs generated from asp.net core. I have created a blank weather forecast service:
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
_logger.LogTrace("LogTrace");
_logger.LogDebug("LogDebug");
_logger.LogInformation("LogInformation");
_logger.LogError("LogError");
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Trace",
"TestAPI": "Trace"
},
"ApplicationInsights": {
"InstrumentationKey": "xx-xx-x-x-xx",
"LogLevel": {
"Default": "Trace",
"Microsoft": "Trace",
"TestAPI": "Trace"
}
}
},
"AllowedHosts": "*"
}
I have following nuget installed both version 2.14.0: Microsoft.Extensions.Logging.ApplicationInsights Microsoft.ApplicationInsights.AspNetCore
Now I try to run the app, but gets no logs.
I try adding services.AddApplicationInsightsTelemetry(); to startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry();
services.AddControllers();
}
Same no logs.
First, please note that the ApplicationInsights
key means two very different things depending on where it's located in the JSON file.
If the key is on the JSON root level (ie. what you call "outside"), it's used to configure Application Insights, and it's where you specify your instrumentation key. It looks like this:
{
"ApplicationInsights": {
"Instrumentationkey":"xxx-36a5-4687-b1fc-xxxxxx"
}
}
Second, if it's located inside the Logging
section, it's used to configure the ApplicationInsightsLoggerProvider
, which determines which log level is sent to Application Insights. That's the ILogger
log filtering mechanism.
By default, only log levels warning
or higher are sent to app insights. If you only want to send all your logs to application insights, you can either configure it for your namespaces, or ignore the messages coming from the System
and Microsoft
namespaces:
{
"Logging": {
"ApplicationInsights": {
"LogLevel": {
"Default": "Trace"
"System": "None",
"Microsoft": "None"
}
}
}