简体   繁体   English

Azure 函数中的 EventGridTrigger - 日志依赖注入不起作用

[英]EventGridTrigger in Azure function - Log dependency injection not working

Here is development environment version details这是开发环境版本的详细信息

  • .Net Core Version : 2.2 .Net 核心版本:2.2
  • Visual Studio : 2019视觉工作室:2019

Here is the startup.cs class这是startup.cs

[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这是EventGridTrigger

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但是我通过传递 ILogger 用Log替换了_log ,它只是有效

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

why does ILogger<DataProcessingFunction> log is not working?为什么ILogger<DataProcessingFunction> log不起作用?

Update 0228:更新 0228:

This issue only occurs in azure portal, but can work locally.此问题仅发生在 azure 门户中,但可以在本地工作。 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):一件有趣的事情是日志没有显示在日志控制台中,但它确实显示在日志流控制台中(在 azure 门户 -> 功能应用 -> 平台功能 -> 日志流中):

在此处输入图片说明

在此处输入图片说明


Original answer:原答案:

This issue is caused by the namespace which has the character "."此问题是由具有字符"."namespace引起的"." . .

If the namespace of azure function has the character "."如果 azure 函数的namespace有字符"." (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"): (在您的情况下,它是PaaS.Azure.Functions ),然后您需要在host.json文件中指定如下所示的 LogLevel(记住右键单击host.json文件 -> 选择属性 -> 设置“复制到输出目录”如“如果更新则复制”):

{
  "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 "."如果 azure 函数的namespace没有字符"." (assume the namespace is MyFunctionApp ), then you don't need the above settings, and the logs are always printed out. (假设命名空间是MyFunctionApp ),那么就不需要上面的设置了,日志总是打印出来的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM