Reputation: 5221
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.
Upvotes: 0
Views: 874
Reputation: 14472
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"
}
}
}
Upvotes: 1
Reputation:
First of all, there is no need to put those provider settings outside "Logging" (you shouldn't).
Every logging setting you want should be put inside only ( unless there is a provider configured to specifically read that)
Now to answer your question, let's say that your application's root namespace is MyNetCore
. ( it would be similar to the name of the project).
If you are using Visual Studio, You can view your Project's root namespace from Project Properties -> Application -> Default Namespace
To view logs from your application only, you have to set the default logging level to None
and logging level of your project MyNetCore
to Trace
[Edit: You have set the logging levels for ApplicationInsights
(or any other provider) separately. The default one is for kestrel
.]
"Logging": {
"LogLevel": {
"Default": "None",
"MyNetCore": "Trace"
},
"ApplicationInsights": {
"InstrumentationKey": "xxx-36a5-4687-b1fc-xxxxxx",
"LogLevel": {
"Default": "None",
"MyNetCore": "Trace"
}
}
}
The Logging level set here is the
minimum
level.
If you set it to Trace (0), all the log levels (greater than 0) will be shown. i.e. FromInformation
toCritical
If you set it to None (6), no logs will be shown
Reference to different Log Levels : https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loglevel
If you want to view only errors from asp.net core, and every log level from your application, then you can do this.
"Logging": {
"LogLevel": {
"Default": "None",
"Microsoft": "Error",
"MyNetCore": "Trace"
},
"ApplicationInsights": {
"InstrumentationKey": "xxx-36a5-4687-b1fc-xxxxxx",
"LogLevel": {
"Default": "None",
"Microsoft": "Error",
"MyNetCore": "Trace"
}
}
}
Edit: To read the above ApplicationInsights
configuration, you need to have Microsoft.Extensions.Logging.ApplicationInsights nuget package installed. Otherwise the config will be totally ignored.
Upvotes: 0