kudlatiger
kudlatiger

Reputation: 3258

EventGridTrigger in Azure function - Log dependency injection not working

Here is development environment version details

Here is the startup.cs class

[assembly: WebJobsStartup(typeof(Startup))]

namespace PaaS.Azure.Functions
{
  public class Startup : IWebJobsStartup
  {
    public void Configure(IWebJobsBuilder builder)
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

        builder.Services
            .AddSingleton<IConfiguration>(config)
            .AddSingleton(serviceProvider => serviceProvider)
            .AddLogging();
      }
  }
}

Here is the EventGridTrigger class

public class DataProcessingFunction
{
    private readonly ILogger _log;
    public DataProcessingFunction(ILogger<DataProcessingFunction> log)
    {
        _log = log;
    }

    [FunctionName("DataProcessingFunction")]
    public void Run([EventGridTrigger]EventGridEvent eventGridEvent)
    {
            _log.LogInformation("Triggered");
    }
}

Logs are not printing

But I replace _log with Log by passing ILogger, it just works

public void Run([EventGridTrigger]EventGridEvent eventGridEvent, ILogger Log)
{
         Log.LogInformation("Triggered");
}

why does ILogger<DataProcessingFunction> log is not working?

Upvotes: 0

Views: 393

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29940

Update 0228:

This issue only occurs in azure portal, but can work locally. One interesting thing is that the logs are not shown in Log Console, but it does show in Log Streaming console(in azure portal -> function app -> Platform features -> Log streaming):

enter image description here

enter image description here


Original answer:

This issue is caused by the namespace which has the character "." .

If the namespace of azure function has the character "."(in your case, it's PaaS.Azure.Functions), then you need to specify the LogLevel like below in host.json file(remember that right click the host.json file -> select Properties -> set "Copy to Output Directory" as "Copy if newer"):

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "PaaS.Azure.Functions": "Information"
    }
  }
}

Then you can see the logs are printed out.

If the namespace of azure function does not have the character "."(assume the namespace is MyFunctionApp), then you don't need the above settings, and the logs are always printed out.

Upvotes: 2

Related Questions