我对 Azure Functions 使用 DI 模式,因此,我创建了如下所示的启动文件:
[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
namespace MyNamespace
{
public sealed class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddLogging();
builder.Services.AddTransient<IMyManager, Mymanager>();
...
}
}
}
和 Azure 函数:
public sealed class MyFunc
{
public MyFunc(IMyManager manager)
{
_myManager = manager;
}
[FunctionName("MyFunc")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
ILogger log, ExecutionContext context)
{
log.LogInformation("this is a test"); // this log appear in Application Insights
...
}
和 MyManager:
public sealed class MyManager: IMyManager
{
public MyManager(ILogger<MyManager> log)
{
_log = log;
}
public async Task ExecuteAsync(...)
{
_log.LogInformation("Execute MyManager"); // this log does not appear in app insights
}
}
似乎 ILogger 或任何注入(inject)的记录器没有反射(reflect)在 Application Insights 中。我应该在 host.json 文件中定义它吗?
我认为应该可以正常工作,无需进行更改。
最佳答案
如 documentation provided by Microsoft 中所述您必须配置您的 host.json:
“host.json 文件配置决定函数应用向 Application Insights 发送多少日志记录。”
你至少应该enable the critical level日志记录。
关于c# - 如果在 Azure Function C# 中使用依赖项注入(inject),ILogger 不会写入 Application Insight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64496421/