如何使用IWebJobsStartup使用新的DI将ILogger注入到Azure函数中? [英] How can I use the new DI to inject an ILogger into an Azure Function using IWebJobsStartup?
问题描述
我正在使用Azure Function
v2.这是我使用构造函数注入的函数:
I am using Azure Function
v2. Here is my function that uses the constructor injection:
public sealed class FindAccountFunction
{
private readonly IAccountWorkflow m_accountWorkflow;
private readonly IMapper m_mapper;
private readonly ILogger m_logger;
public FindAccountFunction(ILogger logger, IMapper mapper, IAccountWorkflow accountWorkflow)
{
m_logger = logger;
m_mapper = mapper;
m_accountWorkflow = accountWorkflow;
}
[FunctionName("FindAccount")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, Verbs.Get, Route = "v1/accounts/")] HttpRequest httpRequest, ILogger logger)
{
// Do stuff.
}
}
我在要从IWebJobsStartup
派生的Startup类中声明了要注入到Azure函数中的所有依赖项:
I am declaring all the dependencies that I want to inject into my Azure Function in the Startup class that derives from IWebJobsStartup
:
public sealed class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder webJobsBuilder)
{
// Registers the application settings' class.
webJobsBuilder.Services.AddSingleton<IApplicationSettings, ApplicationSettings>();
// ** Registers the ILogger instance **
// ** ?? **
// Registers the IMapper instance for the contracts.
var mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile(new MyProfile()));
webJobsBuilder.Services.AddSingleton(mapperConfiguration.CreateMapper());
// Registers custom services.
webJobsBuilder.Services.AddTransient<IStorageService, StorageService>();
webJobsBuilder.Services.AddTransient<IAccountWorkflow, AccountWorkflow>();
}
}
Azure函数调用也依赖于ILogger的其他注入服务,例如IAccountWorkflow
:
The Azure Function calls other injected services that do depends on the ILogger as well, such as the IAccountWorkflow
:
public sealed class AccountWorkflow : IAccountWorkflow
{
public AccountWorkflow(ILogger logger, IStorageService storageService)
{
if(logger is null)
throw new ArgumentNullException();
}
}
问题在于,由于注入了空ILogger
,所以DI无法找到任何ILogger
实现,并且无法解析服务.
The problem is that the DI is unable to find any ILogger
implementation and fails to resolve services since a null ILogger
is injected.
问题
如何在IWebJobsStartup
中设置ILogger
的注入?
推荐答案
UPDATE
注册服务
要注册服务,可以创建一个configure方法并将组件添加到
IFunctionsHostBuilder
实例. Azure Functions主机创建一个IFunctionsHostBuilder
并将其直接传递到您配置的方法中.
Registering services
To register services, you can create a configure method and add components to an
IFunctionsHostBuilder
instance. The Azure Functions host creates anIFunctionsHostBuilder
and passes it directly into your configured method.
要注册您的configure方法,必须添加一个程序集属性
使用以下命令为您的configure方法指定类型
FunctionsStartup
属性.
To register your configure method, you must add an assembly attribute
that specifies the type for your configure method using the
FunctionsStartup
attribute.
所以在这种情况下
[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
namespace MyNamespace {
public class Startup : FunctionsStartup {
public override void Configure(IFunctionsHostBuilder builder) {
// ** Registers the ILogger instance **
builder.Services.AddLogging();
// Registers the application settings' class.
//...
//...omitted for brevity
}
}
}
原始
我相信,由于您有权访问服务集合,因此您应该能够向其中添加日志记录
ORIGINAL
I believe since you have access to the service collection, you should be able to add logging to it
public void Configure(IWebJobsBuilder webJobsBuilder) {
// ** Registers the ILogger instance **
webJobsBuilder.Services.AddLogging();
//OR
//webJobsBuilder.Services.AddLogging(builder => {
// //...
//});
// Registers the application settings' class.
//...
//...removed for brevity
}
,并且在函数的构造函数中有一个ILoggerFactory
.
and having anILoggerFactory
in the Function's constructor.
//...
//Ctor
public FindAccountFunction(ILoggerFactory loggerFactory, IMapper mapper, IAccountWorkflow accountWorkflow) {
m_logger = loggerFactory.CreateLogger<FindAccountFunction>();
m_mapper = mapper;
m_accountWorkflow = accountWorkflow;
}
//...
这篇关于如何使用IWebJobsStartup使用新的DI将ILogger注入到Azure函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!