4

I have certian code in ConfigureAppConfiguration method I want to add few logs here I tried writing below code but it fails how can I fix this? Or is there any other way to do logging in this method:

public static IWebHostBuilder AddKeyVault(this IWebHostBuilder builder)
{
    return builder.ConfigureAppConfiguration(
        (context, config) =>
        {
            var loggerFactory = builder.Build().Services.GetRequiredService<ILoggerFactory>(); // code fails here
            var logger = loggerFactory.CreateLogger(typeof(WebHostBuilderCustomExtension));
            //  if (context.HostingEnvironment.IsProduction())
            {
                var cert = new X509Certificate2(File.ReadAllBytes("keyvaultcertificate.pfx"));
            
                var builtConfig = config.Build();
                
                config.AddAzureKeyVault(builtConfig["vaultname"], "8c372a04-8578-4c38-a58d-a821d85212cb",cert);
                logger.LogInformation($"connected to key vault {builtConfig["azure.keyvault.uri"]}");
            }

        });
}

I get the error below when I run this code:

enter image description here

This is how this method is called:

return WebHost.CreateDefaultBuilder(args).AddKeyVault()
    .UseStartup<Startup>();
olfek
  • 3,210
  • 4
  • 33
  • 49
ankush
  • 949
  • 2
  • 14
  • 33
  • 1
    It fails **how**? – Peter Bons Aug 02 '19 at 11:47
  • If you say something like "it fails" or "there's an error", that should immediately and always be followed with a description of the problem and/or exception and stack trace. – Chris Pratt Aug 02 '19 at 13:41
  • How did you call `return builder.ConfigureAppConfiguration`? Share us your current program.cs. What is the relationship between your first part and last part code? – Edward Aug 05 '19 at 07:13
  • @TaoZhou program.cs contains only last part of code , i updated first part of code it is under extension method AddKeyVault() which i created – ankush Aug 05 '19 at 08:58

1 Answers1

3

For IWebHostBuilder, you could not build it twice as the error indicates.

For ServiceCollection, you will not be able to access the services like ILoggerFactory before build the host.

For a workaround, you will need to initialize your own ILoggerFactory manually like.

public static class WebHostBuilderCustomExtension
{
    public static IWebHostBuilder AddKeyVault(this IWebHostBuilder builder)
    {
        return builder.ConfigureAppConfiguration(
                        (context, config) =>
                        {
                            var loggerFactory = new LoggerFactory(); 
                            loggerFactory.AddConsole();
                            var logger = loggerFactory.CreateLogger(typeof(WebHostBuilderCustomExtension));
                            logger.LogInformation($"connected to key vault ");
                        });
    }
}
Edward
  • 28,296
  • 11
  • 76
  • 121
  • loggerFactory.AddConsole(); gives an obsolete warning how to handle that? – ankush Aug 05 '19 at 10:49
  • @ankush `loggerFactory.AddConsole();` is a demo code which is used to add `ConsoleLoggerProvider`, add your own logerProvider. – Edward Aug 06 '19 at 02:00