4

FunctionsStartup在 Azure Functions 项目中使用来设置 IoC 绑定。但是,当我在 Azure 中运行它时,从注入创建的任何日志ILogger<T>都不会出现。

我用一个全新的示例项目创建了一个非常精简的版本来演示这一点......

https://github.com/dracan/AzureFunctionsLoggingIssue

这个输出是...

2020-04-03T20:20:35  Welcome, you are now connected to log-streaming service. The default timeout is 2 hours. Change the timeout with the App Setting SCM_LOGSTREAM_TIMEOUT (in seconds). 
2020-04-03T20:20:54.643 [Information] Executing 'TestQueueTriggerFunction' (Reason='New queue message detected on 'myqueue'.', Id=2f13c4c7-8a35-4614-a768-1c3fecea8c31)
2020-04-03T20:20:54.654 [Information] Start of function (this log works)
2020-04-03T20:20:54.655 [Information] End of function (this log also works)
2020-04-03T20:20:54.655 [Information] Executed 'TestQueueTriggerFunction' (Succeeded, Id=2f13c4c7-8a35-4614-a768-1c3fecea8c31)

请注意日志条目“此日志未出现!” MyClass.DoSomething()没有出现。

4

2 回答 2

8

看起来这是一个已知问题。引用微软在 Github 上的回复:

这是关于控制台/调试日志如何在门户中工作的另一个微妙之处。它仅在知道日志消息来自此函数时才显示日志消息——这意味着它们与类别 Function.{FunctionName}.User 匹配。我们传入的 ILogger 会自动使用此类别,但您使用外部记录器记录的任何内容都不会使用此类别。我们这样做是为了您不会在此视图中被背景消息淹没——不幸的是,它也会过滤掉您自己的自定义记录器。

我在使用一种潜在的解决方法跟踪此问题时遇到了问题:Azure/azure-functions-host#4689 (comment)

于 2020-04-08T10:53:00.520 回答
1

下面的快速示例 - 使用ILoggerFactory而不是 ILogger<>

public class LoggingTests
{
    ILogger _log;
    public LoggingTests(ILoggerFactory loggerFactory)
    {
        _log = loggerFactory.CreateLogger(this.GetType());
    }

    [FunctionName("LoggingTests")]
    public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log)
    {
        _log.LogInformation("LogInformation");
        _log.LogWarning("LogWarning");
        _log.LogDebug("LogDebug");
        _log.LogTrace("LogTrace");
        _log.LogError("LogError");
        _log.LogCritical("LogCritical");

        return new OkResult();
    }
}

还要检查host.json文件中的日志级别

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "default": "Trace"
    },
    "applicationInsights": {
      "samplingExcludedTypes": "Request",
      "samplingSettings": {
        "isEnabled": true
      }
    }
  },
}
于 2021-05-05T16:11:16.917 回答