0

I am using the azure functions for a table insert event trigger, specifically the durable functions, and I'm trying to to produce some logs in my activity function.

The problem is that the activity function does not receive any "ILogger", nor does the orchestrator, thus I don't have any access and cannot produce logs for debug.

The overall flow is:

HTTP request => Duarble HTTP starter => Durable functions orchestrator => Durable function activity.

Is there a way to create a logger instance for some class deriving from ILogger? Or perhaps a way to pass the ILogger instance from the HTTP starter down to the activity function?

Any solution would be much appreciated!

Martin
  • 378
  • 2
  • 18

1 Answers1

6

By default the ILogger instance is injected in your functions, unless you are using DI.

All you need to do is Use the ILogger which is injected into your functions. Example:

[FunctionName("Demo")]
public async static Task RunOrchestrator(
    [OrchestrationTrigger] DurableOrchestrationContext context,
    ILogger log)
{
    log.LogInformation("Starting Orchestration function");
}

Incase if you using Dependency injection you should just do the below in your startup builder.Services.AddLogging();

More Information :

https://github.com/Azure/azure-functions-host/issues/4858

https://github.com/Azure/azure-functions-host/issues/2720#issuecomment-503627355

Microsoft Docs

Community
  • 1
  • 1
HariHaran
  • 3,642
  • 2
  • 16
  • 33
  • 1
    By default the ILogger isn't injected in the Durable activity function. (It is howerver in the orchestration), so where should the "builder.Services.AddLogging()" be added? – Martin Sep 02 '19 at 06:14
  • Inorder to use `builder.Services.AddLogging()` you will need to add `Statup` class and make the function as dependency injected one – HariHaran Sep 02 '19 at 06:32
  • @HariHaran I've implemented DI for durable functions as well but the line above does not seem to work. Can't inject an ILogger via the function or the constructor? Any ideas? – Billy Ray Valentine Nov 07 '19 at 11:18
  • 1
    @BillyRayValentine please raise a question with more error info,i'll check it out. – HariHaran Nov 07 '19 at 11:25
  • Hi @HariHaran, please see question here: https://stackoverflow.com/questions/58752340/ilogger-not-injected-in-durable-functions-v2-0 – Billy Ray Valentine Nov 07 '19 at 15:49
  • Does ILogger create any text file or only it writes console in the Azure portal? – Vaibhav More Jun 27 '20 at 05:04
  • I suggest you also take a look at the `CreateReplaySafeLogger(logger)` extension method. This will ensure that each of your messages are logged only once in the orchestrator lifecycle. – Pavisa Jun 22 '21 at 12:00