'See multiple ways to obtain ILogger in Azure Functions 6 from docs and templates, what are the differences?

I'm using Azure Functions 6 (.NET 6, isolated), with Application Insights enabled.

From different versions of Visual Studio, Azure Functions Core Tools, the default Microsoft templates and other docs showed a variety of ways to obtain ILogger. I've seen:

  1. Constructor injection of ILoggerFactory (from a default MS template):
private readonly ILogger _logger;

public MyFunction(ILoggerFactory loggerFactory)
{
    _logger = loggerFactory.CreateLogger<MyFunction>();
}
  1. Getting it from the FunctionContext (also from default MS template):
public async Task<HttpResponseData> Run([HttpTrigger(...)] HttpRequestData req, FunctionContext executionContext)
{
    var logger = executionContext.GetLogger("MyFunction);
}
  1. Injecting ILogger directly into the Run method:
public static void Run(EventGridEvent eventGridEvent, ICollector<string> outputSbMsg, ILogger logger)

  1. Regular ASP.NET Core constructor injection of ILogger<T>:
private readonly ILogger<MyFunction> _logger;

public MyFunction(ILogger<MyFunction> logger)
{
    _logger = logger;
}

What are the differences of all these methods and which ones are the best to use?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source