C# Masstransit日志中间件
我在应用中间件登录masstransit时遇到问题。我想记录发布到总线上的每条消息。所以我遵循我在这里找到的这些步骤: 然后我就这样结束了:C# Masstransit日志中间件,c#,rabbitmq,asp.net-web-api2,masstransit,C#,Rabbitmq,Asp.net Web Api2,Masstransit,我在应用中间件登录masstransit时遇到问题。我想记录发布到总线上的每条消息。所以我遵循我在这里找到的这些步骤: 然后我就这样结束了: public static class MassTransitLoggerExtenions { public static void UseLogger<T>(this IPipeConfigurator<T> configurator,ILoggingBusControl loggingBusControl)
public static class MassTransitLoggerExtenions
{
public static void UseLogger<T>(this IPipeConfigurator<T> configurator,ILoggingBusControl loggingBusControl)
where T : class, PipeContext
{
configurator.AddPipeSpecification(new ExceptionLoggerSpecification<T>(loggingBusControl));
}
}
public class ExceptionLoggerSpecification<T> :
IPipeSpecification<T>
where T : class, PipeContext
{
private readonly ILoggingBusControl loggingBusControl;
public ExceptionLoggerSpecification(ILoggingBusControl loggingBusControl)
{
this.loggingBusControl = loggingBusControl;
}
public IEnumerable<ValidationResult> Validate()
{
return Enumerable.Empty<ValidationResult>();
}
public void Apply(IPipeBuilder<T> builder)
{
builder.AddFilter(new ExceptionLoggerFilter<T>(loggingBusControl));
}
}
public class ExceptionLoggerFilter<T> : IFilter<T> where T : class, PipeContext
{
private readonly ILoggingBusControl loggingBusControl;
public ExceptionLoggerFilter(ILoggingBusControl loggingBusControl)
{
this.loggingBusControl = loggingBusControl;
}
public void Probe(ProbeContext context)
{
}
public async Task Send(T context, IPipe<T> next)
{
throw new Exception("Foo");
try
{
await next.Send(context);
}
catch (Exception ex)
{
}
}
}
用法
消息被正确发布,并且调用了
ExceptionLoggerFilter
,ExceptionLoggerSpecification
构造函数,但是ExceptionLoggerFilter
中的Probe
和Send
方法从未被调用。我做错了什么?您没有指定是否尝试将中间件用于正在发送的消息和正在使用的消息,但从您的问题来看,您似乎尝试将其用于正在发送的消息
如果是这种情况(或者对于遇到相同问题的任何人),请注意,中描述的中间件仅适用于正在使用的消息,而不适用于正在发送的消息
对于记录消息,特别是正在发送的消息,最好使用他在评论中提到的@Alexey Zimarev。您没有指定是否尝试使用中间件来记录正在发送的消息和正在使用的消息,但从您的问题来看,您似乎尝试将其用于正在发送的消息 如果是这种情况(或者对于遇到相同问题的任何人),请注意,中描述的中间件仅适用于正在使用的消息,而不适用于正在发送的消息
对于记录消息,尤其是发送的消息,最好使用他在评论中提到的@Alexey Zimarev。在这个
ILOGingBusControl
东西中配置总线的意义是什么,不管它是做什么的,在容器注册中?ILOGingBusControl
是跨多个web服务共享的。只是为了集中记录。但这对我的问题没有影响。没有它,这也不行。自定义扩展方法的代码是什么?我也发布了。看看MassTransitLoggerExtensions
classI可能会认为OnActivated
并不是一个好的选择。我还建议为此目的使用审计。您只需要实现IMessageAuditStore
接口(它有一种方法)。在ilogingbuscontrol
中配置总线的意义是什么,不管它用于什么,在容器注册中?ilogingbuscontrol
是跨多个web服务共享的。只是为了集中记录。但这对我的问题没有影响。没有它,这也不行。自定义扩展方法的代码是什么?我也发布了。看看MassTransitLoggerExtensions
classI可能会认为OnActivated
并不是一个好的选择。我还建议为此目的使用审计。您只需要实现IMessageAuditStore
接口(它有一个方法)。
public class LoggingBusControl : ILoggingBusControl
{
private readonly IBusControl busControl;
public LoggingBusControl()
{
busControl = GetBusControl();
busControl.Start();
}
private static IBusControl GetBusControl()
{
var busControl = Bus.Factory.CreateUsingRabbitMq(x =>
{
var host = x.Host(new Uri("rabbitmq://localhost/#/queues/%2F/logging_queue"), h =>
{
h.Username("guest");
h.Password("guest");
});
});
return busControl;
}
public void Log<T>(T log) where T : ILog
{
busControl.Publish<ILog>(log);
}
}
builder.Register(context =>
{
var busControl = Bus.Factory.CreateUsingRabbitMq(rabbitMqConfig =>
{
var host = rabbitMqConfig.Host(new Uri(ConfigurationManager.AppSettings["RabbitMQHost"]), h =>
{
h.Username("guest");
h.Password("guest");
});
var logger = context.Resolve<ILoggingBusControl>();
rabbitMqConfig.UseLogger(logger);
});
return busControl;
})
.SingleInstance()
.As<IBusControl>()
.As<IBus>()
.OnActivated(args => args.Instance.Start());
public OrderController(IOrderService orderService,IBusControl busControl)
{
this.orderService = orderService;
this.busControl = busControl;
}