0

I have an Azure Function App project with the following files:

Startup.cs: Registers a dependency

[assembly: FunctionsStartup(typeof(MyLoggingFunction.Startup))]

namespace MyLoggingFunction
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddScoped<MyService>();
        }
    }
}

MyService.cs: Writes a log message

namespace MyLoggingFunction
{
    public class MyService
    {
        private readonly ILogger<MyService> logger;

        public MyService(ILogger<MyService> logger)
        {
            this.logger = logger;
        }

        public void Go()
        {
            this.logger.LogInformation("MyService.Go");
        }
    }
}

MyFunction.cs: The actual function; uses MyService

namespace MyLoggingFunction
{
    public class MyFunction
    {
        private readonly MyService myService;

        public MyFunction(MyService myService)
        {
            this.myService = myService;
        }

        [FunctionName("MyFunction")]
        public IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            this.myService.Go();
            log.LogInformation("All done");
            return new OkObjectResult("Done.");
        }
    }
}

Here is the output visible in Azure after the function runs successfully. Note that the log message from the injected dependency is missing:

log messages

How do I get log messages from the dependency to show up as part of the built-in logging?

Dave Mateer
  • 17,608
  • 15
  • 96
  • 149

2 Answers2

1

Add logging level entry to host.json :

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "default": "Information"
    }
  }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
0

You must whitelist the classes and/or namespaces from which you want to allow logging. An example hosts.json file:

{
  "logging": {
    "logLevel": {
      "MyLoggingFunction": "Information"
    }
  },
  "version": "2.0"
}

This is documented as an issue on the azure-function-host GitHub repository.

Dave Mateer
  • 17,608
  • 15
  • 96
  • 149