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:
- Constructor injection of
ILoggerFactory
(from a default MS template):
private readonly ILogger _logger;
public MyFunction(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<MyFunction>();
}
- 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);
}
- Injecting
ILogger
directly into theRun
method:
public static void Run(EventGridEvent eventGridEvent, ICollector<string> outputSbMsg, ILogger logger)
- 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?