我正在写一个函数应用程序,它修剪到这个
namespace myNamespace {
public class Test {
private readonly ILogger _Constructorlog;
public Test(ILogger<TestProcessor> logger) {
_Constructorlog = logger;
}
[FunctionName("TestLog")]
public async Task<IActionResult> RunWeb([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger methodLog) {
methodLog.LogInformation("method log");
_Constructorlog.LogInformation("Constructor log");
}
}
}
这段代码按照预期将这个写入控制台窗口
[2022-07-06T10:07:35.302Z] method log
Where should "Constructor log"会写吗?
调用LogInformation
时,_Constructorlog
类型为ILogger<TestProcessor>
。
这是"主机"。json文件的
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
}
我也试着用各种JSONhttps://learn.microsoft.com/en us/aspnet/core/fundamentals/logging/?view=aspnetcore - 6.0
需要将命名空间添加到类的host.json
中
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"logLevel": {
"default": "None",
"myNamespace": "Information"
}
}
}
None
在这里停止依赖注入ILogger
.LogInformation
显示在控制台窗口,所以method log
不写入。
将Information
添加到这里的命名空间意味着该命名空间的ILogger<TCategoryName>
和.LogInformation
将写入控制台窗口。
删除"default": "None",
将两者都写入控制台窗口。
我需要测试这些设置上传到Azure时如何应用,但这在本地工作。