使用新的 DI 功能 - Azure Functions 时不会注入 ILogger



使用新的 DI 功能且未在控制台中显示日志信息时,不会注入ILogger

public class SampleGreeter : IGreeter
{
private readonly ILogger<SampleGreeter> logger;
public SampleGreeter(ILogger<SampleGreeter> logger)
{
this.logger = logger;
}
public string CreateGreeting(string name)
{
logger.LogInformation("Logging from greeter");
return $"Hello, {name}. I'm a sample greeter.";
}
}

从欢迎程序不记录任何内容,而从函数运行时记录工作并显示在控制台中。

host.json文件:

{
"version": "2.0",
"logging": {
"applicationInsights": {
"fileLoggingMode": "debugOnly",
"logLevel": {
"default": "Information",
"<namespace>": "Information"
},
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
}
}

Microsoft文档说: "主机将 ILogger 和 ILoggerFactory 服务注入构造函数。但是,默认情况下,这些新的日志记录筛选器将从函数日志中筛选出来。您需要修改 host.json 文件以选择加入其他筛选器和类别。
Microsoft文档和示例

你已经把

"logLevel": {
"default": "Information",
"<namespace>": "Information"
},

(和fileLoggingMode)在applicationInsights级别中,但这不是正确的:它应该高一个级别,如示例配置文件中所示:

"logging": {
"fileLoggingMode": "debugOnly",
"logLevel": {
"default": "Information",
"<namespace>": "Information"
},
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
}

最新更新