代码之家  ›  专栏  ›  技术社区  ›  Pingpong

通过构造函数为带有azure函数2.x的http触发器函数注入ilogger

  •  0
  • Pingpong  · 技术社区  · 5 年前

    ILogger 可以注入函数参数,如 Token 方法如下。

    但是,在将其注入构造函数参数时发生以下错误 log .

    [2019年3月7日17:15:17]执行“令牌”(失败, ID=4E22B21F-97F0-4AB4-8F51-8651B 09AEDC8)[2019年3月7日17:15:17] Microsoft.Extensions.DependencyInjection.Abstractions:无法 正在解析“Microsoft.Extensions.Logging.iLogger”类型的服务 试图激活“功能”。

    逃避者 可以注射到 令牌 下面是函数参数。但是上面的错误是在它被注入构造函数参数时发生的 日志 .

    public class Functions
    {
        private HttpClient _httpClient;
        private IAppSettings _appSettings;
        private ILogger _log;
    
        public Functions(HttpClient httpClient, IAppSettings appSettings  //working for these two
          , ILogger log  //not working, errors
        )
        {
    
            _log = log;
        }
    
        [FunctionName("Token")]
        public async Task<IActionResult> Token(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "Token")]
            HttpRequest httpRequest,
            ILogger log)
        {
    
        }
    }
    

    下面的依赖注入

    [assembly: WebJobsStartup(typeof(Startup))]
    namespace MyApp
    {
        public class Startup : IWebJobsStartup
        {
            public void Configure(IWebJobsBuilder builder)
            {
                builder.Services.AddHttpClient();
                builder.Services.AddTransient<IAppSettings, AppSettings>();     
                 //builder.Services.AddLogging();  //not working
               //builder.Services.AddSingleton<ILogger>() //not working
            }
    }
    

    视觉工作室2017

    0 回复  |  直到 5 年前
        1
  •  0
  •   Kzryzstof    5 年前

    我也有这个问题。我打电话就把它修好了 AddLogging() :

    [assembly: WebJobsStartup(typeof(Startup))]
    namespace MyApp
    {
        public class Startup : IWebJobsStartup
        {
            public void Configure(IWebJobsBuilder builder)
            {
                builder.Services.AddHttpClient();
                builder.Services.AddTransient<IAppSettings, AppSettings>();     
                builder.Services.AddLogging();
            }
    }
    

    然后,在azure函数中,我必须通过 ILoggerFactory 而不是 ILogger 得到 逃避者 来自的实例 loggerFactory :

    public class Functions
    {
        private HttpClient _httpClient;
        private IAppSettings _appSettings;
        private ILogger _log;
    
        public Functions(HttpClient httpClient, IAppSettings appSettings, ILoggerFactory loggerFactory)
        {
            _log = loggerFactory.CreateLogger<Functions>();
        }
    
        [FunctionName("Token")]
        public async Task<IActionResult> Token(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "Token")]
            HttpRequest httpRequest)
        {
               // No need to keep getting the ILogger from the Run method anymore :)
        }
    }
    
        2
  •  2
  •   Milen Stefanov    5 年前

    打电话 LogCategories.CreateFunctionUserCategory 解决了我的问题。完整示例:

    Azure Functions Core Tools (2.7.1158 Commit hash: f2d2a2816e038165826c7409c6d10c0527e8955b)
    Function Runtime Version: 2.0.12438.0
    

    创业公司

    无需添加 builder.Services.AddLogging(); 它会自动导入到容器中。

    using Microsoft.Azure.Functions.Extensions.DependencyInjection;
    using Microsoft.Extensions.DependencyInjection;
    
    [assembly: FunctionsStartup(typeof(MyFunctionsNamespace.Startup))]
    
    namespace MyFunctionsNamespace
    {
        public class Startup : FunctionsStartup
        {
            public override void Configure(IFunctionsHostBuilder builder)
            {
                builder.Services.AddTransient<IMyService, MyService>();
            }
        }
    }
    

    myfunkyFunction.cs

    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.Extensions.Logging;
    
    namespace MyFunctionsNamespace
    {
        public class MyFunkyFunction
        {
            private readonly IMyService _myService;
    
            public MyFunkyFunction(IMyService myService)
            {
                _myService = myService;
            }
    
            [FunctionName("FunkyFunc")]
            public async Task<IActionResult> Run(
                [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
                HttpRequest req
                , ILogger log
            )
            {
                log.LogInformation("C# HTTP trigger function processed a request.");
    
                _myService.Do();
    
                return new OkObjectResult("Hello");
            }
        }
    }
    

    imyservice.cs公司

    任何东西 logcategories.createFunctionUserCategory 会完成任务的。这似乎是一些webjob遗留的需求。

    using Microsoft.Azure.WebJobs.Logging;
    using Microsoft.Extensions.Logging;
    
    namespace MyFunctionsNamespace
    {
        public interface IMyService
        {
            void Do();
        }
    
    
        public class MyService : IMyService
        {
            private readonly ILogger _log;
    
            public MyService(ILoggerFactory loggerFactory)
            {
                // Important: Call CreateFunctionUserCategory, otherwise log entries might be filtered out
                // I guess it comes from Microsoft.Azure.WebJobs.Logging
                _log = loggerFactory.CreateLogger(LogCategories.CreateFunctionUserCategory("Common"));
            }
    
            public void Do()
            {
                _log.Log(LogLevel.Information, "Hello from MyService");
            }
        }
    }