I cannot get basice LogTrace(...)
output in my application. Here's a repro:
- Create a new ASP.NET Core application using Visual Studio 2017.
- (Optional) comment out
.UseApplicationInsights()
so the repro is clearer Replace the code in
ValuesController.cs
with this:using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace WebApplication1.Controllers { [Route("api/[controller]")] public class ValuesController : Controller { private readonly ILogger<ValuesController> logger; public ValuesController(ILogger<ValuesController> logger) { this.logger = logger; } [HttpGet] public IEnumerable<string> Get() { logger.LogError("ERROR!"); logger.LogWarning("WARN!"); logger.LogInformation("INFO!"); logger.LogTrace("TRACE!"); return new string[] { "value1", "value2" }; } } }
Change
appsettings.Development.json
to this:{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Trace", "System": "Information", "Microsoft": "Information" } } }
Run and view the Debug output
This leads to:
I've tried tweaking the values in the appsettings.json
file as well, but that had no effect either.
Weirdly, changing the value in either file to "Error"
doesn't do anything either.
Bottom Line / Question
What do I need to do to make my injected ILogger<ValuesController>
respect the logging settings, including Trace
level?
Footnote
Here's some of the relevant code that would be auto-generated with the above repro:
Startup.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
Program.cs
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
}
appsettings.json
default:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
Where the problem lies...
Based on your
Configure()
method, I have spotted an issue:This is the reason why none of your changes to the configuration set in the
appsettings.json
files is not working.If you want to explicitly set it to use a particular minimum LogLevel, then you can pass it directly to the
AddDebug(ILoggerFactory, LogLevel)
method.More information can be found here.
Binding it to your configuration.
Method 1: Grab the value from the configuration.
Method 2: Use the built-in object for LogLevel
(Intentionally left out. Obviously it sits snug between these two methods offered.) I would favor one of the extremes than to go halfway)
Method 3: Go Manual (use ConfigurationBinder)
The fancy
ConfigurationBinder
which will map to an object like
so you could then pass:
Special note about nodes and appsettings.json
Note that the delimiter for the configuration uses
:
.Example:
"Logging:LogLevel"
will go:LogLevel Enum
Just for reference, here are the valid
LogLevel
values:Source:
https://learn.microsoft.com/en-us/aspnet/core/api/microsoft.extensions.logging.loglevel#Microsoft_Extensions_Logging_LogLevel