'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:
- 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
ILoggerdirectly into theRunmethod:
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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
