title: Masstransit logging middleware
body: <p>I have problem with applying middleware for logging in masstransit. I wanted to log every message which is published to the bus. So I follow these steps which I found here: <a href="http://masstransit-project.com/MassTransit/advanced/middleware/custom.html" rel="nofollow noreferrer">http://masstransit-project.com/MassTransit/advanced/middleware/custom.html</a> 
  and then I endup with something like this:</p>
  
  <pre><code>public static class MassTransitLoggerExtenions
  {
      public static void UseLogger&lt;T&gt;(this IPipeConfigurator&lt;T&gt; configurator,ILoggingBusControl loggingBusControl)
          where T : class, PipeContext
      {
          configurator.AddPipeSpecification(new ExceptionLoggerSpecification&lt;T&gt;(loggingBusControl));
      }
  }
  
  
   public class ExceptionLoggerSpecification&lt;T&gt; :
      IPipeSpecification&lt;T&gt;
      where T : class, PipeContext
  {
      private readonly ILoggingBusControl loggingBusControl;
  
      public ExceptionLoggerSpecification(ILoggingBusControl loggingBusControl)
      {
          this.loggingBusControl = loggingBusControl;
      }
      public IEnumerable&lt;ValidationResult&gt; Validate()
      {
          return Enumerable.Empty&lt;ValidationResult&gt;();
      }
  
      public void Apply(IPipeBuilder&lt;T&gt; builder)
      {
          builder.AddFilter(new ExceptionLoggerFilter&lt;T&gt;(loggingBusControl));
      }
  
  }
  
   public class ExceptionLoggerFilter&lt;T&gt; : IFilter&lt;T&gt; 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&lt;T&gt; next)
      {
          throw new Exception("Foo");
          try
          {
              await next.Send(context);
          }
          catch (Exception ex)
          {
  
          }
      }
  
  
  }
  </code></pre>
  
  <p>There is my simple abstraction over <code>IBusControl</code></p>
  
  <pre><code> public class LoggingBusControl : ILoggingBusControl
  {
      private readonly IBusControl busControl;
      public LoggingBusControl()
      {
          busControl = GetBusControl();
          busControl.Start();
      }
  
      private static IBusControl GetBusControl()
      {
          var busControl = Bus.Factory.CreateUsingRabbitMq(x =&gt;
          {
              var host = x.Host(new Uri("rabbitmq://localhost/#/queues/%2F/logging_queue"), h =&gt;
              {
                  h.Username("guest");
                  h.Password("guest");
              });
          });
          return busControl;
      }
  
  
      public void Log&lt;T&gt;(T log) where T : ILog
      {
          busControl.Publish&lt;ILog&gt;(log);
      }
  }
  </code></pre>
  
  <p>Usage</p>
  
  <pre><code> builder.Register(context =&gt;
              {
                  var busControl = Bus.Factory.CreateUsingRabbitMq(rabbitMqConfig =&gt;
                  {
                      var host = rabbitMqConfig.Host(new Uri(ConfigurationManager.AppSettings["RabbitMQHost"]), h =&gt;
                      {
                          h.Username("guest");
                          h.Password("guest");
                      });
                      var logger = context.Resolve&lt;ILoggingBusControl&gt;();
                      rabbitMqConfig.UseLogger(logger);
                  });
  
                  return busControl;
              })
              .SingleInstance()
              .As&lt;IBusControl&gt;()
              .As&lt;IBus&gt;()
              .OnActivated(args =&gt; args.Instance.Start());
  </code></pre>
  
  <p>Then I inject IBusControl in controller constructor</p>
  
  <pre><code> public OrderController(IOrderService orderService,IBusControl busControl)
      {
          this.orderService = orderService;
          this.busControl = busControl;
  
      }
  </code></pre>
  
  <p>Messages are publised correctly and <code>ExceptionLoggerFilter</code>, <code>ExceptionLoggerSpecification</code> constructors are called, but methods <code>Probe</code> and <code>Send</code> in <code>ExceptionLoggerFilter</code> are never invoked. What I am doing wrong ?</p>