Editorials

Implement Logging Without Library Dependencies

Recently I’ve been working on implementing logging in a new application. Logging is a common feature we find ourselves constantly implementing. I hope were not implementing it from scratch. There are a number of logging libraries, especially in Dot Net, that handle just about any logging need you can think of.

This time, rather than write software that is directly linked to an implementation, it seemed like a good thing to have a wrapper, or interface, that the software would call, having difference logging implementations. In this fashion, the logging contained in the application would be able to be written to the interface, and the actual implementation could be replaced if there was a need, without a huge impact on the application.

I started writing a generic interface with the intention of implementing the ability to inject Log4Net and NLog, since my company is using both libraries. Moving forward we could use the generic implementation, and swap out Log4Net with NLog, or even something new, when the business requirements change.

As I was starting to design this new interface I was very excited to find that Dot Net Standard, as implemented in Dot Net Core, has an implementation that already does this. Not only do they have the generic implementation, they have been working with a number of the leading logging library implementations, including NLog and Log4Net, assuring that the popular tools work well with this new library.

Another thing that’s nice about the Dot Net Logging Extensions is that they also have some implementations of basic logging destinations such as files. You can use the native loggers without having to use a third party logging library if it fits your requirements.

There is a great article in on MSDN demonstrating how to implement Logging in Dot Net Core. They cover some loggers build into their library and how to implement calling external loggers.

If you’re not ready to take on Dot Net Core, then you can always go and build yourself a logging wrapper, or find one already written that meets your needs. I’m sure there are some Java and JavaScript utilities out there with similar behaviors for those applications where Dot Net isn’t your coding library.

Feel free to share your favorite logging providers in the comments for others to review.

Cheers,

Ben