I try to add a logger to my console app. All configuration apply correctly, except Logging
Program.cs:
private static void Main(string[] args)
{
var services = new ServiceCollection();
ConfigureServices(services);
var serviceProvider = services.BuildServiceProvider();
serviceProvider.GetService<App>().Run();
Console.ReadKey();
}
private static void ConfigureServices(ServiceCollection services)
{
var envName = Environment.GetEnvironmentVariable("MY_ENV") ?? DevelopmentEnvName;
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false)
.AddJsonFile($"appsettings.{envName}.json", true, true)
.AddEnvironmentVariables()
.Build();
services.AddSingleton(configuration);
services.AddLogging(builder =>
{
builder.AddConfiguration(configuration)
.AddConsole()
.AddDebug();
});
ConfigureOptions(services, configuration);
ConfigureDependencies(services);
}
In App.Run()
I try log debug and info message
public void Run()
{
_logger.LogDebug("Debug");
_logger.LogDebug(_appOptions.Env); //print nothing
_logger.LogInformation("Info");
_logger.LogInformation(_appOptions.Env); //print Info and debug env lines
}
appsettings.json:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information"
}
},
"AppOptions": {
"Env": "none"
}
}
appsettings.Dev.json:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug"
}
},
"AppOptions": {
"Env": "debug env"
}
}
What I need to change to control log level by json settings?