2

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?

Logging in ASP.NET Core

  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>();
    }
}
Joans
  • 31
  • 3
  • Your question is mostly unclear, but in regards to how you can get an `ILogger` instance without DI, the answer is you cannot. You have to be able to inject it. – Chris Pratt Nov 12 '18 at 14:26
  • I using EncryptedJsonConfigurationProvider repleace JsonConfigurationProvider, I want using logger in EncryptedJsonConfigurationProvider, how to get logger? – Joans Nov 12 '18 at 15:58
  • 1
    Any way get logger before WebHostBuilder.Builder? – Joans Nov 12 '18 at 16:00
  • @ChrisPratt if only get Logger instance without di, can refer to https://stackoverflow.com/questions/48676152/asp-net-core-web-api-logging-from-a-static-class , howerver i want get logger when builder.AddJsonConfigurationSourceDecryption() method is calling – Joans Nov 12 '18 at 16:02

1 Answers1

0

@0xced answered this question here How to create a LoggerFactory with a ConsoleLoggerProvider?

as of dotnetcore3.1 the code could be written this way

private static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args).ConfigureAppConfiguration(
                (hostContext, config) =>
                    {
                        var loggerFactory = LoggerFactory.Create(
                            builder => 
                                {
                                    builder
                                        .AddFilter("Microsoft", LogLevel.Warning)
                                        .AddFilter("System", LogLevel.Warning)
                                        .AddFilter("YourProgramNamespsace.Program", LogLevel.Debug)
                                        .AddConsole();
                                });
                        var logger = loggerFactory.CreateLogger<Program>();
                        logger.LogInformation("Hello from ConfigureAppConfiguration");
                    ...
                    })
svonidze
  • 151
  • 8