<bdo id='L9raP'></bdo><ul id='L9raP'></ul>
        <legend id='L9raP'><style id='L9raP'><dir id='L9raP'><q id='L9raP'></q></dir></style></legend>
        <tfoot id='L9raP'></tfoot>
      1. <small id='L9raP'></small><noframes id='L9raP'>

        <i id='L9raP'><tr id='L9raP'><dt id='L9raP'><q id='L9raP'><span id='L9raP'><b id='L9raP'><form id='L9raP'><ins id='L9raP'></ins><ul id='L9raP'></ul><sub id='L9raP'></sub></form><legend id='L9raP'></legend><bdo id='L9raP'><pre id='L9raP'><center id='L9raP'></center></pre></bdo></b><th id='L9raP'></th></span></q></dt></tr></i><div id='L9raP'><tfoot id='L9raP'></tfoot><dl id='L9raP'><fieldset id='L9raP'></fieldset></dl></div>

        ILogger 通过 Azure Function 2.x 的 Http 触发函数的构造函数注入

        ILogger injected via constructor for Http trigger functions with Azure Function 2.x(ILogger 通过 Azure Function 2.x 的 Http 触发函数的构造函数注入)

      2. <tfoot id='ukjut'></tfoot>

          <i id='ukjut'><tr id='ukjut'><dt id='ukjut'><q id='ukjut'><span id='ukjut'><b id='ukjut'><form id='ukjut'><ins id='ukjut'></ins><ul id='ukjut'></ul><sub id='ukjut'></sub></form><legend id='ukjut'></legend><bdo id='ukjut'><pre id='ukjut'><center id='ukjut'></center></pre></bdo></b><th id='ukjut'></th></span></q></dt></tr></i><div id='ukjut'><tfoot id='ukjut'></tfoot><dl id='ukjut'><fieldset id='ukjut'></fieldset></dl></div>

          <small id='ukjut'></small><noframes id='ukjut'>

            <bdo id='ukjut'></bdo><ul id='ukjut'></ul>

                1. <legend id='ukjut'><style id='ukjut'><dir id='ukjut'><q id='ukjut'></q></dir></style></legend>
                    <tbody id='ukjut'></tbody>

                  本文介绍了ILogger 通过 Azure Function 2.x 的 Http 触发函数的构造函数注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  ILogger 可以注入到函数参数中,比如下面的Token方法.

                  ILogger can be injected to function parameter, like Token method below.

                  但是,注入到构造函数参数log时,出现如下错误.

                  However, the error below occurred when it is injected to constructor parameter log.

                  [07/03/2019 17:15:17] 执行令牌"(失败,id=4e22b21f-97f0-4ab4-8f51-8651b 09aedc8) [07/03/2019 17:15:17]Microsoft.Extensions.DependencyInjection.Abstractions:无法解析Microsoft.Extensions.Logging.ILogger"类型的服务,而尝试激活功能".

                  [07/03/2019 17:15:17] Executed 'Token' (Failed, Id=4e22b21f-97f0-4ab4-8f51-8651b 09aedc8) [07/03/2019 17:15:17] Microsoft.Extensions.DependencyInjection.Abstractions: Una ble to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'Functions'.

                  ILogger 可以注入到下面的Token 函数参数中.但是上面的错误是注入到构造函数参数log的时候出现的.

                  ILogger can be injected to Token function parameter below. But the error above occurred when it is injected to constructor parameter log.

                  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

                  Visual studio 2017

                  推荐答案

                  我也遇到了这个问题.我可以通过调用 AddLogging():

                  I had this problem as well. I was able to fix it by calling 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:

                  And then, in the Azure Function, I had to do pass a ILoggerFactory instead of an ILogger and get the ILogger instance from the 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 :)
                      }
                  }
                  

                  这篇关于ILogger 通过 Azure Function 2.x 的 Http 触发函数的构造函数注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding and removing users from Active Directory groups in .NET(在 .NET 中的 Active Directory 组中添加和删除用户)
                  set equality in linq(在 linq 中设置相等)
                  HashSet conversion to List(HashSet 转换为 List)
                  How to set timeout for webBrowser navigate event(如何为 webBrowser 导航事件设置超时)
                  Test whether two IEnumerablelt;Tgt; have the same values with the same frequencies(测试两个IEnumerablelt;Tgt;具有相同频率的相同值)
                  How do you determine if two HashSets are equal (by value, not by reference)?(您如何确定两个 HashSet 是否相等(按值,而不是按引用)?)
                  <tfoot id='DXpYn'></tfoot>
                  1. <legend id='DXpYn'><style id='DXpYn'><dir id='DXpYn'><q id='DXpYn'></q></dir></style></legend>

                    <small id='DXpYn'></small><noframes id='DXpYn'>

                  2. <i id='DXpYn'><tr id='DXpYn'><dt id='DXpYn'><q id='DXpYn'><span id='DXpYn'><b id='DXpYn'><form id='DXpYn'><ins id='DXpYn'></ins><ul id='DXpYn'></ul><sub id='DXpYn'></sub></form><legend id='DXpYn'></legend><bdo id='DXpYn'><pre id='DXpYn'><center id='DXpYn'></center></pre></bdo></b><th id='DXpYn'></th></span></q></dt></tr></i><div id='DXpYn'><tfoot id='DXpYn'></tfoot><dl id='DXpYn'><fieldset id='DXpYn'></fieldset></dl></div>

                          • <bdo id='DXpYn'></bdo><ul id='DXpYn'></ul>
                              <tbody id='DXpYn'></tbody>