c# - 使用 Azure Functions 进行 ILogger 依赖注入(inject)

标签 c# azure dependency-injection azure-functions ilogger

所以我尝试使用依赖注入(inject)设置 ILogger,但遇到了一些麻烦。

在我的startup.cs文件我有一些看起来像这样的东西:

        builder.Services.AddLogging();

然后,我尝试将该记录器提供给我自己的类(其作用类似于 ILogger 库的包装器)

    builder.Services.AddSingleton<LoggingWrapper>(s =>
    {
        return new LoggingWrapper(
            s.GetService<ILoggerFactory>());
    });

所以在我的 Azure 函数中,LoggingWrapper被注入(inject)内部,但它不会记录任何内容。每个函数附带的普通 ILogger 仍然可以工作,但我想知道为什么我的包装器没有记录任何内容。

Logging Wrapper 是一个类,它采用 ILogger 方法并使用它来创建自己的日志记录方法。例如loggingWrapper.logInformation("string")是一个围绕“ILogger”的方法。

我将其注入(inject)到我的其他类中,如下所示:s.GetService<ILoggerFactory>()

预先感谢您的所有帮助!

最佳答案

我遇到了同样的问题 - 您需要设置/允许来自其他来源的日志记录:

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "YourNamespace": "Information"
    }
  }
}

更多信息请参见:

关于c# - 使用 Azure Functions 进行 ILogger 依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63403722/

相关文章:

c# - Automapper 实例配置尚未在 xUnit 测试中初始化

c# - 如何加入 2 个通用 IEnumerators

azure - 逻辑应用 - 使用路径获取 Blob 内容

android - 如何在kodein中使用带有两个参数的工厂绑定(bind)来恢复对象?

dependency-injection - AutoFac 比 Microsoft.Extensions.DependencyInjection 有什么优势?

twig 扩展中的 Symfony 依赖注入(inject)

c# - 将 DataTable 上传到 Azure blob 存储

c# - 用于通过 C#(使用 SSL)运行 FTP 服务器的组件或源代码

azure - 在 Office 365 中更改域名如何影响 Azure Active Directory 连接计算机的用户身份验证?

azure - 未指定默认订阅。使用 Select-AzureSubscription -Default <subscriptionName> 设置默认订阅