I would like to use logger in "TODO" line, because I need to log when I add a "AddJsonConfigurationSourceDecryption" method, Is there any way to get a logger?
public class Program
{
public static void Main(string[] args)
{
ILogger<Program> logger = null;
try
{
var host = CreateWebHostBuilder(args).Build();
ApplicationLogging.LoggerFactory = host.Services.GetRequiredService<ILoggerFactory>();
logger = host.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("start thin API service...");
host.Run();
}
catch (Exception ex)
{
logger?.LogError(ex, "Stopped program because of exception");
throw;
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseKestrel(option => option.AddServerHeader = false)
.ConfigureLogging((host, builder) => { builder.SetMinimumLevel(LogLevel.Trace); })
.UseNLog()
.ConfigureServices(services => services.AddAutofac())
.ConfigureAppConfiguration((context, builder) =>
{
//TODO: Logger.............
builder.SetBasePath(Directory.GetCurrentDirectory());
builder.AddJsonConfigurationSourceDecryption();
})
.UseStartup<Startup>();
}
}
@0xced answered this question here How to create a LoggerFactory with a ConsoleLoggerProvider?
as of dotnetcore3.1 the code could be written this way