Azure Function Ilogger

In this Azure article, we will discuss Azure Function ilogger. And it’s Dependency Injection.

Azure Function Ilogger

While working with the Azure function, sometimes there might be a chance that you will get some errors, warnings, or critical issues from the code. Now, if you want to track those issues and know what and where exactly the problem is, then the ILogger interface is the right choice in this scenario. You can log the details into a log file.

When you log the details into a log file, in case of any issues, you will learn about that by analyzing the log file, which will provide enough information on the root cause. You will also be able to identify the exact function or place where the issue occurs.

Logging is one of the essential components for your Azure function to analyze the behavior and is useful when you need to troubleshoot in case of any issues.

Ilogger is the alternative to TraceWriter that was there before. The TraceWriter is obsolete with Azure Functions version 2.0.

One more benefit of ilogger is it supports structured logging. It supports the below types of logging.

  • Info (LogInformation)
  • Debug (LogDebug)
  • Error (LogError)
  • Warning (LogWarning)
  • Critical (LogCritical)
  • Trace (LogTrace)

Below is an example of the implementation of Ilogger.

namespace AzureFunctionSQL
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            using (var con1 = new SqlConnection("Server=tcp:sql1246.database.windows.net,1433;Initial Catalog=MySQLDB;Persist Security Info=False;User ID=Rajkishore;Password=Tamanna@19;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"))

            {
                try
                {
                    SqlCommand command1 = new SqlCommand();
                    command1.CommandText = "INSERT INTO Customers(customerID, LastName, FirstName) VALUES (15, 'SaiRam', 'SaiRam')";
                    //command1.CommandType = CommandType.StoredProcedure;
                    command1.Connection = con1;
                    con1.Open();
                    command1.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    log.LogInformation(ex.Message);

                }

            }

        }
    }

Azure Function ilogger Dependency Injection

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

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 
    }  
}  

Conclusion

In this article, we discussed Azure Function ilogger. And Azure Function ilogger Dependency Injection. Thanks for reading this article !!!