I was just browsing an Azure Functions documentation page and came across the following example
Isolated Worker model
//<docsnippet_fixed_delay_retry_example>
[Function(nameof(TimerFunction))]
[FixedDelayRetry(5, "00:00:10")]
public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo timerInfo,
FunctionContext context)
{
var logger = context.GetLogger(nameof(TimerFunction));
vs
In-process model
[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
if (myTimer.IsPastDue)
{
log.LogInformation("Timer is running late!");
}
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
I have been using dependency injection (i.e. in-process model approach, although usually constructor based with a non-static function method, rather than via the method signature as shown). I am wondering what the differences are of using the context.GetLogger method instead and if there are any advantages to doing so, other than it seems to allow for slightly terser method signatures for the functions? I assume there are likely other reasons for them to bother including the difference in the code examples.
Azure Functions provide two distinct models for logging within function apps. that are in-process model and the isolated worker model. These two models use for same purpose to logging the functions, but different approaches like below:
In-process model:
Utilize the
loginstance provided by dependency injection for logging messages at different log levels like below:Isolated worker model:
The isolated worker model, which provides greater isolation and scalability, doesn't support dependency injection in the same manner as the in-process model.
Inside the function method, access the logger using
context.GetLogger. TheFunctionContextis provided as a parameter to the function.loggerinstance obtained fromcontext.GetLoggerfor logging messages at different log levels like below:ILoggerwith dependency injection in the in-process model orcontext.GetLoggerin the isolated worker model enables logging within Azure Functions.