I can't seem to get Trace level log information outputted after upgrading to .NET Core 2.0 (+ASP.NET Core 2.0).
In fact, if I do a dotnet new web
project and add the code below in Startup for Configure, I do not get any trace or debug log messages, but I get the Information and Error messages twice. Commenting out the .AddConsole()
call will output these (Information and Error) only once - suggesting that it gets configured automatically with a console provider by default. Keep in mind, this is a "File -> New" project experience, there is nothing setup in Program.cs
for logging or configuration at all for this - except for what I've added. Anyone seen things? Or should I register a GitHub issue for it.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Microsoft.Extensions.Logging.LogLevel.Trace);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
var logger = loggerFactory.CreateLogger("Blah");
logger.LogTrace("Hello world : Trace");
logger.LogDebug("Hello world : Debug");
logger.LogInformation("Hello world : Information");
logger.LogError("Hello world : Error");
await context.Response.WriteAsync("Hello World!");
});
}
Nothing of the above works for me The only workaround was to write a method
and when using the AddLogging extension method write it as
The following structure of
appsettings.json
seems to work fine:Taken from https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1
Also, see what your start up calls are, I find the following works for me:
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 asAnd have an
appsettings.json
likeNotice
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 withSystem.*
.This is for the class used in
T
inILogger<T>
). This allows to set a higher or lower than default logging level for loggers from this namespace.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 theappsettings.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.Update
In case one doesn't want to use the
appsettings.json
, one can register the filters in code too.I spent almost twenty minutes to realize that since
Configuration.GetSection("Logging")
in the Startup.cs file reads the section"Logging"
from the config in the appsettings.json file, which was configured as"Error"
. Changing it to"Information"
or anything lower, fixed the issue.Here's what the appsettinsg.json file looks now:
To find out more about the levels of logging (such as in
"Information"
), check out this link, that also provides general information on ASP.NET Core logging.I'm just posting here, just in case you bump into any trouble with getting the logging to work, make sure you've been through that JSON file.