简体   繁体   English

如何记录 MassTransit 发布的消息/内部错误?

[英]How to log MassTransit published messages/internal errors?

I have a.Net 4.8 web application on IIS that once called uses MassTransit to publish a message to RabbitMQ queue.我在 IIS 上有一个.Net 4.8 web 应用程序,该应用程序曾经调用过使用 MassTransit 将消息发布到 RabbitMQ 队列。 Everything works most of the time but there are cases (like 1 in 10) when I have no idea why nothing is published.大部分时间一切正常,但有些情况(比如十分之一)我不知道为什么什么都没有发布。 I have a debug logging right before/after publish and the "after" is never fired.我在发布之前/之后有一个调试日志,并且永远不会触发“之后”。 No exceptions, no nothing.没有例外,没有什么。

// Constructor, fired on each request - maybe it's better to have static one?
public QueueLibHelper()
{
    var cfg = ConfigHelper.GetConfig();
    bus = Bus.Factory.CreateUsingRabbitMq(x => 
        x.Host(new Uri(cfg.Host),
            h => {
                h.Heartbeat(15);
                h.Username(cfg.UserName); 
                h.Password(cfg.Password);
                if (!String.IsNullOrWhiteSpace(cfg.Nodes))
                {
                    string[] nodes = cfg.Nodes.Split(',');
                    h.UseCluster(c =>
                    {
                        foreach (var n in nodes)
                        {
                            c.Node(n);
                        }
                    });
                }
            }));
    bus.Start();
}

//  Sending to queue, 
public void UpdatePmtToQueue(long paymentId, string status)
{
    var message = new InputData() { Id = paymentId, Status = status };
    MassTransit.Util.TaskUtil.Await(() => bus.Publish<IInputData>(message));
    StopBus();
}

I assume it might be connected to the whole async/await situation.我认为它可能与整个异步/等待情况有关。 I tried also GetAwaiter().GetResult() but that approach had a ton of different problems.我也试过GetAwaiter().GetResult()但这种方法有很多不同的问题。 Maybe bus is somehow closed before the publish actually runs?也许公共汽车在发布实际运行之前以某种方式关闭? I tried to add MT logging like this, but no log is being written:我尝试像这样添加 MT 日志记录,但没有写入日志:

<source name="MassTransit" switchValue="All">
    <listeners>
      <add name="Console" type="MassTransit.Logging.Tracing.ConsoleTraceListener, MassTransit" />
      <add name="Xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="MassTransit.svclog" />
    </listeners>
  </source>

Any ideas what could be wrong or at least how to see what MassTransit is doing (failing) internally?有什么想法可能是错误的,或者至少如何查看 MassTransit 在内部做什么(失败)?

MassTransit uses Microsoft.Extensions.Logging.Abstractions for log output, any logging framework that integrates with it should work. MassTransit 对日志 output 使用Microsoft.Extensions.Logging.Abstractions ,任何与其集成的日志框架都应该可以工作。

It's also typical to start the bus at application start time, and leave it running until the application exits, at which time the bus should be stopped to ensure all pending messages have been delivered.在应用程序启动时启动总线也很典型,并让它一直运行直到应用程序退出,此时应该停止总线以确保所有待处理的消息都已传递。

MassTransit is very async, it uses the TPL heavily. MassTransit 非常异步,它大量使用 TPL。 So if you're trying to cross the chasm from sync to async, TaskUtil.Await() works most of the time but mileage varies.因此,如果您试图跨越从同步到异步的鸿沟,TaskUtil.Await() 大部分时间都可以工作,但里程会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM