[英]NLog with Autofac in MassTransit 6.0?
My Consumers fail to instantiate from Autofac due to the following:由于以下原因,我的消费者无法从 Autofac 实例化:
Cannot resolve parameter 'Microsoft.Extensions.Logging.Logger
1[MyConsumer] logger' of constructor 'Void .ctor(MyService, Microsoft.Extensions.Logging.Logger
1[MyConsumer])'.无法解析
1[MyConsumer] logger' of constructor 'Void .ctor(MyService, Microsoft.Extensions.Logging.Logger
1[MyConsumer])”的参数“Microsoft.Extensions.Logging.Logger1[MyConsumer] logger' of constructor 'Void .ctor(MyService, Microsoft.Extensions.Logging.Logger
”。
The ILoggers resolve fine in every other class except the MassTransit Consumers;除了 MassTransit Consumer 之外,ILoggers 在其他所有类中都能很好地解析; I believe it's an issue with LogContext.ConfigureCurrentLogContext();
我相信这是 LogContext.ConfigureCurrentLogContext() 的问题; and order of operations.
和操作顺序。 Below is the code, edited for brevity:
以下是代码,为简洁起见进行了编辑:
ContainerBuilder builder = new ContainerBuilder(); // Autofac
builder.RegisterType<LoggerFactory>()
.As<ILoggerFactory>()
.SingleInstance();
builder.RegisterGeneric(typeof(Logger<>))
.As(typeof(ILogger<>))
.SingleInstance();
builder.RegisterAssemblyTypes() // load up all services, yada yada
// what to pass here? I'm inside an unbuilt Autofac Container at this point. There is no ILoggerFactory until the Container is built!
LogContext.ConfigureCurrentLogContext(); // configure MassTransit logging
builder.AddMassTransit(x =>
{
x.AddConsumers(Assembly.GetEntryAssembly());
x.AddBus(bus => MassTransit.Bus.Factory.CreateUsingRabbitMq(config =>
{
config.ConfigureEndpoints(bus); // wire-up consumers discovered by the AddConsumers() call above
}));
});
Container = builder.Build(); // now build the container
// add NLog
IServiceProvider serviceProvider = new AutofacServiceProvider(Container); // this requires a built Container!
ILoggerFactory loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
loggerFactory.AddNLog();
I should note that logging within MassTransit is actually working;我应该注意到 MassTransit 中的日志记录实际上是有效的; I see all kind of debug output regarding the queues and messages.
我看到了有关队列和消息的各种调试输出。 But the Consumer will not instantiate for whatever reason.
但是无论出于何种原因,消费者都不会实例化。 If I try to manually
Resolve<ILogger<MyConsumer>>()
I'll get an ILogger but MassTransit seems to have trouble with it.如果我尝试手动
Resolve<ILogger<MyConsumer>>()
我会得到一个 ILogger 但 MassTransit 似乎有问题。
Edit : full error message via VS Output:编辑:通过 VS 输出的完整错误消息:
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MyConsumer' can be invoked with the available services and parameters:
在类型 'MyConsumer' 上找到的带有 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' 的构造函数都不能用可用的服务和参数调用:
Cannot resolve parameter 'Microsoft.Extensions.Logging.Logger 1[MyConsumer] logger' of constructor 'Void .ctor(MyService, Microsoft.Extensions.Logging.Logger`1[MyConsumer])'.*无法解析构造函数“Void .ctor(MyService, Microsoft.Extensions.Logging.Logger`1[MyConsumer])”的参数“Microsoft.Extensions.Logging.Logger 1[MyConsumer] logger”。*
Package versions:包版本:
Cannot resolve parameter
Microsoft.Extensions.Logging.Logger 1[MyConsumer] logger
无法解析参数
Microsoft.Extensions.Logging.Logger 1[MyConsumer] logger
This error message means that Autofac can't find a registered Logger<MyConsumer>
.此错误消息意味着Autofac找不到已注册的
Logger<MyConsumer>
。 If you look at your registration you register ILogger<>
and not Logger<>
.如果您查看您的注册,您注册的是
ILogger<>
而不是Logger<>
。 You should change the dependency to the interface version in MyConsumer
and autofac will find it.您应该将依赖项更改为
MyConsumer
的接口版本, autofac会找到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.