c# - 如果在 Azure Function C# 中使用依赖项注入(inject),ILogger 不会写入 Application Insight

标签 c# azure azure-functions

我对 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/

相关文章:

c# - 获取枚举的自定义属性的属性

c# - 滚动单击不适用于 NavigationManager.NavigateTo,我无法使用 href,因为我需要 forceLoad 功能

powershell - 通过 Azure 资源管理器应用多个 DSC

java - 在 Java 中使用 Visual Studio Code 的 Azure Functions 项目

python - 为什么 python azure 函数无法在本地运行?

c# - 如何获得属性(property)的 setter/getter ?

javascript - 数据表格式无效: must have at least 2 columns

azure - ASP.NET 5 (RC1) 错误网关 : The specified CGI application encountered an error and the server terminated the process

c# - 将凭据从 Web 应用程序传递到 Web API。 Azure AD 身份验证

Azure函数应用程序读取O365组邮箱