How can I use the new DI to inject an ILogger into an Azure Function using IWebJobsStartup?(如何使用新的 DI 将 ILogger 注入使用 IWebJobsStartup 的 Azure 函数?)
问题描述
我正在使用 Azure 函数 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();
}
}
问题在于 DI 无法找到任何 ILogger 实现并且无法解析服务,因为注入了一个空的 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 的注入?
How can I setup the injection of the ILogger in IWebJobsStartup?
推荐答案
更新
参考 使用依赖注入在 .NET Azure 函数中
要注册服务,您可以创建配置方法并将组件添加到 IFunctionsHostBuilder实例.Azure Functions 主机创建一个 IFunctionsHostBuilder并将其直接传递到您配置的方法中.
Registering services
To register services, you can create a configure method and add components to an
IFunctionsHostBuilderinstance. The Azure Functions host creates anIFunctionsHostBuilderand passes it directly into your configured method.
要注册你的配置方法,你必须添加一个程序集属性指定您的配置方法的类型,使用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 => {
// //...
/
本文标题为:如何使用新的 DI 将 ILogger 注入使用 IWebJobsStartup 的 Azure 函数?
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
