The way logging is configured has changed a little... The recommended way (and it's pretty well documented in this GitHub issue/announcement to do it now is to configure the loggers on the AddLogging
method, such as
services.AddLogging(builder =>
{
builder.AddConfiguration(Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug();
});
And have an appsettings.json
like
Notice
Seems a few people are confused, because the example only demonstrates the configuration of Console
provider and not all loggers.
The LogLevel
section configures logging level for all namespaces (Default
key) or for a specific namespace (System
overrides the default value for all classes logging whose namespace starts with System.*
.
This is for the class used in T
in ILogger<T>
). This allows to set a higher or lower than default logging level for loggers from this namespace.
{
"ApplicationInsights": {
"InstrumentationKey": ""
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information",
"System": "Warning",
"Microsoft": "Information"
},
"Console": {
"LogLevel": {
"Default": "Warning",
"System": "Information",
"Microsoft": "Information"
}
}
}
}
Please note that the structure of the appsettings.json changed from what it used to be in .NET Core 1.x and that Logging
entry in the appsettings.json
now has logger provider names in it, which allows you to configure logging levels per logging provider.
Previously, the entry in appsettings.json
would only be applicable to the console logger.
Alternatively, the logging can now be moved within the WebHostBuilder
instead.
public static void Main()
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile("hosting.json", optional: false)
.AddEnvironmentVariables();
})
.ConfigureLogging((webhostContext, builder) => {
builder.AddConfiguration(webhostContext.Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug();
})
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
Update
In case one doesn't want to use the appsettings.json
, one can register the filters in code too.
services.AddLogging(builder =>
{
builder.AddConfiguration(Configuration.GetSection("Logging"))
// filter for all providers
.AddFilter("System", LogLevel.Debug)
// Only for Debug logger, using the provider type or it's alias
.AddFilter("Debug", "System", LogLevel.Information)
// Only for Console logger by provider type
.AddFilter<DebugLoggerProvider>("System", LogLevel.Error)
.AddConsole()
.AddDebug();
});
will give me them once
What do you mean by that? – mjwillsset ASPNETCORE_ENVIRONMENT=Development; dotnet run
– Martin Ullrichawait Task.Delay(1)
after the logging statements gets the Console to display logs. If It take it out, only partial logs are displayed on the Console. The accepted answer doesn't actually do anything. It seems to be a threading issue. – bloudraak