5

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>();
4
  • 1
    It fails how? Commented Aug 2, 2019 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. Commented Aug 2, 2019 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? Commented Aug 5, 2019 at 7: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 Commented Aug 5, 2019 at 8:58

1 Answer 1

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 ");
                        });
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

loggerFactory.AddConsole(); gives an obsolete warning how to handle that?
@ankush loggerFactory.AddConsole(); is a demo code which is used to add ConsoleLoggerProvider, add your own logerProvider.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.