ILogger injected via constructor for Http trigger functions with Azure Function 2.x(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 触发函数的构造函数注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ILogger 通过 Azure Function 2.x 的 Http 触发函数的构造函数注入
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01