Logger – Part 2

Last time I laid out a super quick customization for the Microsoft.Extensions.Logging NUGET package. The code I presented lives in one of my NUGET packages, named CG.Logging.Bus, which can be downloaded for free from: https://github.com/CodeGator/CG.Logging.Bus

At the end of my last article I promised that I would demonstrate how to take that CG.Logging.Bus package and use it to integrate with a Rabbit MQ server, and also a MSMQ server. Let’s look at that now.

Don’t worry. This is easy stuff this time.

Let’s start with the Rabbit MQ variant.

Quick flashback: If you’ll recall, the BusLoggerProvider class from my last article was responsible for creating instances of my custom BusLogger class. That class has a virtual method called Initialize, which is called from within the extension classes (ILoggerFactoryExtensions and ILoggingBuilderExtensions) in order to make sure the provider is ready for operation. That virtual method, Initialize, by itself, simply creates an in-memory version of my IBus component. That’s fine for testing and very (VERY) light duty logging, but it’s obviously not appropriate for production use. So, to fix that, we’ll derive from the BusLoggerProvider class now and create a new provider we’ll call RabbitBusLoggerProvider.

Here is a look at the code for the RabbitBusLoggerProvider class:


See? I told you this was going to be simple …

As we can see, the RabbitBusLoggerProvider class derives from our BusLoggerProvider base class. It contains a single constructor that’s really just used to pass parameters to that base class. It also contains the override for the Initialize method.

Inside our Initialize override, we create an instance of my BusBuilder object, then we inject a setup for my RabbitMQ IBus library. Afterwords, we call the SetQueueName and SetHostName methods to pass the host name and the queue name from the settings object into my IBus’s setup. Finally, we call the build method on my builder and initialize the Bus property with the results.

So, simply overriding the Initialize method is enough to get a completely different messaging strategy out of this package.

How would we go about using this Rabbit MQ variation in your project? Just like last time, there are two extension methods, on the ILoggerFactory and ILoggingBuilder types.

Here is the code for the ILoggerFactory extension:


Here is the code for the ILoggingBuilder extension:


Both of these methods works just like the ones I covered in the previous article. They both create a provider object, initialize that provider, then inject it into the factory (or builder). The difference, in this case is that these methods inject a RabbitBusLoggerProvider object, whereas the extension methods in the previous article injected an instance of BusLoggerProvider, instead.

Now that I’ve shown how to adapt things for use with a Rabbit MQ setup, let’s look at how to do the same thing with a Microsoft MSMQ setup:


Once again, we simply derive a new provider class, this time called MicrosoftBusLoggerProvider, from our BusLoggerProvider base and override the Initialize method. Inside that method, we take the few steps needed to create a MSMQ variant of my IBus component and set the reference to that object in the provider’s Bus property.

How would we go about using this MSMQ variation in your project? As always, there are two extension methods, on the ILoggerFactory and ILoggingBuilder types.

Here is the code for the ILoggerFactory extension:


Here is the code for the ILoggingBuilder extension:


This should all be very familiar at this point. These extension methods inject a MSMG variant of my IBus object, through the overridden Initialize method in the provider. Once that’s done, the provider is injected into the factory (or builder), and at that point we’re ready to start logging!



So, in this article I’ve introduced two extensions for logging through a Rabbit MQ or MSMQ messaging architecture. The only thing left to do is cover how we might actually use these extensions in a project. Let’s look at that now.

Here is an example of using our new extension methods to wire up a Rabbit MQ logger for a console application:


Here is the same example for wiring up a MSMQ logger:


Here is an example for a .NET Core web application, this time using the Rabbit MQ variant:


Here is the same example, this time using the MSMQ variant:



If you’ve read all the way through this article then I hope you’ve come away with an appreciation for just how easy it is to adapt the ILogger framework in the Microsoft.Extensions.Logging NUGET package. The possibilities are really endless, just use your own imagination. Now get out there and start logging!


The code for this article is taken from my NUGET packages: CG.Logging.Bus.Microsoft and CG.Logging.Bus.Rabbit, which can both be downloaded for free at:




The source code for the CG.Logging.Bus.Microsoft and CG.Logging.Bus.Rabbit projects live on Github and can be obtained for free at: