【问题标题】:Serilog in Azure FunctionsAzure Functions 中的 Serilog
【发布时间】:2019-04-30 08:25:46
【问题描述】:

Azure Functions 中的每个方法都可以将Microsoft.Extensions.Logging.ILogger 注入其中以进行日志记录。将 WebJobsStartup 与启动类一起使用,您可以使用以下语法将日志记录更改为使用 Serilog:

[assembly: WebJobsStartup(typeof(Startup))]
namespace MyFuncApp {
    public class Startup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.AddLogging(
                lb => lb.ClearProviders()
                    .AddSerilog(
                        new LoggerConfiguration()
                            .Enrich.FromLogContext()
                            .WriteTo.Console()
                            .WriteTo.File(@"C:\Temp\MyFuncApp.log")
                            .CreateLogger(),
                        true));
        }
    }
}

我还可以将其他对象添加到 DI 中,并将它们注入方法或包含使用方法的类的构造函数中,例如 builder.Services.AddSingleton<IMyInterface, MyImplementation>();

但是,我非常希望能够以相同的方式注入 Microsoft.Extensions.Logging.ILogger,但如果我尝试在构造函数中使用 ILogger,我会在方法调用期间收到以下错误(因为那是类已创建):

Microsoft.Extensions.DependencyInjection.Abstractions:尝试激活“MyFuncApp.MyFunctions”时,无法解析“Microsoft.Extensions.Logging.ILogger”类型的服务。

那么,有没有办法将ILogger 注入到这样的类构造函数中?

public class MyFunctions
{
    private IMyInterface _myImpl;
    private ILogger _log;

    public MyFunctions(
        IMyInterface myImplememtation, // This works
        ILogger log) // This does not
    {
        _myImpl = myImplementation;
        _log = log;
        _log.LogInformation("Class constructed");
    }

    public async Task<IActionResult> Function1([HttpTrigger() ... ) {
        _log.LogInformation("Function1 invoked");
    }
}

【问题讨论】:

    标签: c# azure dependency-injection azure-functions serilog


    【解决方案1】:

    可以通过使用包Anotar.Serilog.Fody(以及任何其他Anotar包)进一步简化必要的设置

    您需要在Startup 类中设置同样的 Serilog。

    但是,使用Fody 包,您可以完全摆脱注入的记录器

    using Anotar.Serilog;
    
    public class Functions
    {
        [FunctionName("Token")]
        public async Task<IActionResult> Function1(
            [HttpTrigger()]...)
        {
            // static calls to the LogTo class
            // get translated into proper Serilog code during build
            LogTo.Information("Function1 invoked");
        }
    }
    

    【讨论】:

      【解决方案2】:

      请尝试下面的代码,它在我身边有效:

          [assembly: WebJobsStartup(typeof(Startup))]
          namespace MyApp
       {
              public class Startup : IWebJobsStartup
              {
                  public void Configure(IWebJobsBuilder builder)
                  {
                      //other code
      
                      builder.Services.AddLogging();
                  }
              }
      
      
      
          public class Functions
          {
              //other code
              private ILogger _log;
      
              public Functions(ILoggerFactory loggerFactory)
              {
                  _log = loggerFactory.CreateLogger<Functions>();
              }
      
              [FunctionName("Token")]
              public async Task<IActionResult> Function1(
                  [HttpTrigger()]...)
              {
                     _log.LogInformation("Function1 invoked");
              }
          }
      
      }
      

      【讨论】:

      • 你在哪里用LoggerConfiguration配置Serilog?
      • @GTHvidsten 请看看这个article。如果有任何问题,请告诉我,然后我回来时看看。谢谢。
      • ILoggerFactory.CreateLogger() 在构造函数中似乎工作得很好,如果我在方法中使用生成的记录器,则功能似乎与将 ILogger 直接注入方法相同。感谢您的帮助:)
      • 你试过了吗:public Functions(ILogger&lt;Functions&gt; logger)
      【解决方案3】:

      使用 AzureFunctions v3,您在问题中概述的模式开箱即用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-05
        • 2020-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-03
        • 2017-10-29
        • 2016-08-26
        相关资源
        最近更新 更多