如何在隔离的 .NET Core 6 上运行的 Azure Function 中的所有类中访问 ILogger

问题描述 投票:0回答:1

我正在将 Web 作业转换为 Azure 函数。我创建了一个与 .NET 6 隔离的 Azure Function。我无法从类构造函数访问 ILogger,出现此错误:

System.InvalidOperationException:无法解析类型的服务 尝试时出现“Microsoft.Extensions.Logging.LoggerFactory” 激活“ABC”

这是我的代码:

namespace MyFunction
{
    public class MyFunctionProcessor
    {
        private readonly ILogger log;
        private IABC _iABC;

        public MyFunctionProcessor(ILoggerFactory logger, IABC _iABC)
        {
            log = logger.CreateLogger<MyFunctionProcessor>();
            this._iABC = _iABC;
        }

        [Function(nameof(MyFunctionProcessor))]
        public async Task Run([EventHubTrigger("%EventHubName%", 
            Connection = "ServiceBusConnectionString")] EventData[] events, PartitionContext partitionContext)
        {
            Guid correlationId = Guid.NewGuid();
            log.LogInformation($"CorrelationId: {correlationId} ABC Processor Function has been started");

            try
            {
                await this._iABC.SyncDataAsync(events, partitionContext);
            }
            catch (Exception ex)
            {
                log.LogError($"CorrelationId: {correlationId} Exception while processing the event {ex.Message} => {ex.StackTrace}");
            }
        }
    }
}

ABC 类文件:

namespace MyFunction.Services
{
    public class ABC : IABC
    {
         private IService serv;
         private ILogger log;

         public ABC(IService _serv, LoggerFactory _log)
         {
              serv = _serv;
              log =_log.CreateLogger<ABC>();
         }
         
          public async Task SyncDataAsync(EventData[] messages, PartitionContext context)
          {
              // code logic
              var xyz = serv.getDetails();          
              // code logic
          }
    }
}

Program.cs
文件:

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
    services.AddApplicationInsightsTelemetryWorkerService();
    services.ConfigureFunctionsApplicationInsights();
    services.AddScoped<IABC, ABC>();
    services.AddScoped<IService, Service>();
    services.AddLogging();
    services.AddSingleton<ILoggerFactory, LoggerFactory>();
    services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
})
.Build();

host.Run();

类库服务代码:

namespace ClassLibrary1
{
    public class Service : IService
    {
         private ILogger log;

         public Service(LoggerFactory _log)
         {
              log =_log.CreateLogger<Service>();
         }
         
         public async Task getDetails()
         {
             // code logic
             // here I'm logging the details
         }
    }
}

我该如何解决这个问题?

c# dependency-injection .net-6.0 ilogger azure-functions-isolated
1个回答
0
投票

ILoggerFactory
已注册用于依赖注入 (DI)。但是
ABC
的构造函数

public ABC(IService _serv, LoggerFactory _log)
{
     serv = _serv;
     log =_log.CreateLogger<ABC>();
}

需要

LoggerFactory
。该构造函数参数类型需要更改为
ILoggerFactory

但是,不需要使用

ILoggerFactory
ILogger<T>
将可用于 DI,因此可以避免在构造函数中使用工厂。例如,
ABC
的构造函数可以更改为:

private readonly ILogger<ABC> _logger;
public ABC(IService _serv, ILogger<ABC> logger)
{
     serv = _serv;
     _logger = logger;
}

也可能有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.