user11631417
user11631417

Reputation:

Get Reference to ILogger in Azure Function Startup.cs

In an Azure Function project is there a way to get a reference to ILogger inside the Configure() method of Startup.cs?

(I need to log some initialization steps that happen during the configuration hook)

public class StartUp : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
       //get reference to ILogger Here
    }
}

Upvotes: 2

Views: 1500

Answers (1)

HariHaran
HariHaran

Reputation: 4199

You can make use of the LoggerFactory to create an Instance of Ilogger in your startup. Here's an working example for you.

public class Startup : FunctionsStartup
    {
        private ILoggerFactory _loggerFactory;
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var config = new ConfigurationBuilder()
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            builder.Services.AddLogging();
            ConfigureServices(builder);

        }

        public void ConfigureServices(IFunctionsHostBuilder builder)
        {
            _loggerFactory = new LoggerFactory();
            var logger = _loggerFactory.CreateLogger("Startup");
            logger.LogInformation("Got Here in Startup");

            //Do something with builder
        }
    }

Upvotes: 1

Related Questions