[英]EventGridTrigger in Azure function - Log dependency injection not working
Here is development environment version details这是开发环境版本的详细信息
Here is the startup.cs
class这是
startup.cs
类
[assembly: WebJobsStartup(typeof(Startup))]
namespace PaaS.Azure.Functions
{
public class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
var config = new ConfigurationBuilder()
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
builder.Services
.AddSingleton<IConfiguration>(config)
.AddSingleton(serviceProvider => serviceProvider)
.AddLogging();
}
}
}
Here is the EventGridTrigger
class这是
EventGridTrigger
类
public class DataProcessingFunction
{
private readonly ILogger _log;
public DataProcessingFunction(ILogger<DataProcessingFunction> log)
{
_log = log;
}
[FunctionName("DataProcessingFunction")]
public void Run([EventGridTrigger]EventGridEvent eventGridEvent)
{
_log.LogInformation("Triggered");
}
}
Logs are not printing
日志不打印
But I replace _log
with Log
by passing ILogger, it just works但是我通过传递 ILogger 用
Log
替换了_log
,它只是有效
public void Run([EventGridTrigger]EventGridEvent eventGridEvent, ILogger Log)
{
Log.LogInformation("Triggered");
}
why does ILogger<DataProcessingFunction> log
is not working?为什么
ILogger<DataProcessingFunction> log
不起作用?
Update 0228:更新 0228:
This issue only occurs in azure portal, but can work locally.此问题仅发生在 azure 门户中,但可以在本地工作。 One interesting thing is that the logs are not shown in Log Console, but it does show in Log Streaming console(in azure portal -> function app -> Platform features -> Log streaming):
一件有趣的事情是日志没有显示在日志控制台中,但它确实显示在日志流控制台中(在 azure 门户 -> 功能应用 -> 平台功能 -> 日志流中):
Original answer:原答案:
This issue is caused by the namespace
which has the character "."
此问题是由具有字符
"."
的namespace
引起的"."
. .
If the namespace
of azure function has the character "."
如果 azure 函数的
namespace
有字符"."
(in your case, it's PaaS.Azure.Functions
), then you need to specify the LogLevel like below in host.json
file(remember that right click the host.json
file -> select Properties -> set "Copy to Output Directory" as "Copy if newer"): (在您的情况下,它是
PaaS.Azure.Functions
),然后您需要在host.json
文件中指定如下所示的 LogLevel(记住右键单击host.json
文件 -> 选择属性 -> 设置“复制到输出目录”如“如果更新则复制”):
{
"version": "2.0",
"logging": {
"logLevel": {
"PaaS.Azure.Functions": "Information"
}
}
}
Then you can see the logs are printed out.然后你可以看到日志被打印出来了。
If the namespace
of azure function does not have the character "."
如果 azure 函数的
namespace
没有字符"."
(assume the namespace is MyFunctionApp
), then you don't need the above settings, and the logs are always printed out. (假设命名空间是
MyFunctionApp
),那么就不需要上面的设置了,日志总是打印出来的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.