【问题标题】:.NET Core 3 Azure Function ILogger - correct use?.NET Core 3 Azure Function ILogger - 正确使用?
【发布时间】:2021-02-01 03:26:45
【问题描述】:

我有一个 Azure 函数。在我的 startup.cs 我有

builder.Services.AddLogging();

在我的功能中:

public void MyFunction(blah blah, ILogger log) {
    // this works fine, Hello world appears in the function output
    log.LogInformation("Hello world");
}

但是,如果我尝试通过 ctor 将 ILogger 日志注入到类库中,我会得到一个空值。 因此,我改为使用 ClassName 注入 ILogger,这似乎工作正常,例如

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

    public void MyMethod() {
        // this logs, but it doesn't log to the azure function output
        _log.LogInformation("Inside a class library");
    }
}

我的问题是,该类的任何日志消息都不会出现在该函数的日志输出中。我需要添加什么才能做到这一点?

【问题讨论】:

  • 您是否已在 host.json 中定义了默认日志级别并将 APPINSIGHTS_INSTRUMENTATIONKEY 设置为您的函数应用中的应用程序设置?检查thisworking repo 以获取更多配置

标签: .net-core azure-functions


【解决方案1】:

尝试将logging 部分添加到您的host.json 文件中:

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

MySolutionName 替换为您的解决方案的全名。这应该会在logLevelInformation 及以上启用日志记录。

【讨论】:

  • 谢谢,我明天试试,我已经有了这个,但是解决方案名称错误,因为我在创建它后更改了解决方案名称。祈祷这就是它!
  • 不,这还没有解决,我的应用程序还是一样的。 _log.LogWarning("测试");如果它在类库中则不起作用。那就是注入 ILogger 日志;
猜你喜欢
  • 1970-01-01
  • 2018-03-13
  • 2019-03-13
  • 2021-05-02
  • 2020-04-21
  • 2021-07-09
  • 2019-06-27
  • 2020-05-18
  • 1970-01-01
相关资源
最近更新 更多