Dependency injection of ILogger vs context.GetLogger in Azure Functions Isolated worker

55 Views Asked by At

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.

1

There are 1 best solutions below

0
Pavan On

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:

  • In the in-process model, passing an ILogger instance as a parameter directly into the function method.
[FunctionName("TimerTrigger")]
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}");
}

Utilize the log instance provided by dependency injection for logging messages at different log levels like below:

log.LogInformation("Processing started.");
log.LogError("An error occurred: {errorMessage}", ex.Message);

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. The FunctionContext is provided as a parameter to the function.

[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));
}
  • Utilize the logger instance obtained from context.GetLogger for logging messages at different log levels like below:
logger.LogInformation("Processing started."); 
logger.LogError("An error occurred: {errorMessage}", ex.Message);
  • Using ILogger with dependency injection in the in-process model or context.GetLogger in the isolated worker model enables logging within Azure Functions.
  • For this logging difference Refer this DOC