Azure Functions - 注入的 ILogger<T> 日志没有出现
Azure Functions - Injected ILogger<T> logs aren't appearing
我在 Azure Functions 项目中使用 FunctionsStartup
来设置 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()
中的日志条目 "This log doesn't appear!" 没有出现。
看起来这是一个已知问题。引用微软Github的回复:
This is another subtlety about how that console/debug log works in the portal. It only displays log messages if it knows they come from this function -- which means they match the category Function.{FunctionName}.User. The ILogger we pass in uses this category automatically but anything you log with an external logger will not use this. We do this so that you don't get flooded with background messages in this view -- and unfortunately it filters out your own custom logger as well.
I've got an issue tracking this with one potential workaround: Azure/azure-functions-host#4689 (comment).
下面的快速示例 - 使用 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
}
}
},
}
我在 Azure Functions 项目中使用 FunctionsStartup
来设置 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()
中的日志条目 "This log doesn't appear!" 没有出现。
看起来这是一个已知问题。引用微软Github的回复:
This is another subtlety about how that console/debug log works in the portal. It only displays log messages if it knows they come from this function -- which means they match the category Function.{FunctionName}.User. The ILogger we pass in uses this category automatically but anything you log with an external logger will not use this. We do this so that you don't get flooded with background messages in this view -- and unfortunately it filters out your own custom logger as well.
I've got an issue tracking this with one potential workaround: Azure/azure-functions-host#4689 (comment).
下面的快速示例 - 使用 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
}
}
},
}