Azure Function Logging Dependency Injection

Recently, I worked for a client requirement where I implemented Azure Function Logging with Dependency Injection. This article will discuss that functionality.

Azure Function Logging Dependency Injection

You can use the ilogger interface in your startup class. Then, use the “LoggerFactory” class to create the instance for the ilogger interface.

Check out the sample code below to help you implement Azure Function ilogger dependency injection.

public class Startup: FunctionsStartup {  
    private ILoggerFactory _loggerFactory;  
    public override void Configure(IFunctionsHostBuilder builder) {  
        var myconfig = 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("I am in Startup");  
        //builder implementation goes here 
    }  
}  

You may also like following the below-related articles

Conclusion

This is how you can implement the Azure Function Logging Dependency Injection. Thanks for reading this article !!!