Use MongoDB to store Sitecore log entries centrally!

Posted 02/24/2015 by Akshay Sura

The idea is to store a copy of the Sitecore logs in a central location such as a common MongoDB. Why? hmm why not? If you are running multiple CD servers and you want to view the logs for all of them in one location, might be one reason. The reason will become apparent in the coming days/weeks!

For now, the process. I used the following blog posts as reference:

1. Anders Laub – Working with custom MongoDB collections in Sitecore 8 using WebApi

2. Thomas Stern – Storing Sitecore log events in MongoDB

First a class representing the data we want to store:

Next, we need the LogRepository which will interact with MongoDB. You can use reflector to see how Sitecore is currently doing this for tracking and analytics:

Next, we need a class MongoLogger, implementing the AppenderSkeleton from the log4net implementation tucked away in Sitecore.Logging.dll. We will pass in the connection string name and the collection name from the config.

Once all the above are done, we get to the configs. 

Add a connection string to the new MongoDB.

Here what I did is to define an Appender called MongoDBAppender for log4net calling the MongoLogger:

I also added a Logger which by default inherits the root. It’s generally not a good practice to add to the root node. 

The MongoDBAppender logger is explicitly configured to log to MongoDBAppender, and is configured not to inherit settings from parent loggers (additivity=”false”). Therefore it doesn’t log to LogFileAppender. If you set additivity=”true” it will inherit settings and log to both LogFileAppender and MongoDBAppender.

Here we added the MongoDBAppender followed by LogFileAppender. What this does is that it duplicates the entries in to the MongoDB, which is exactly what I am looking for.

Final product, log entries in the MongoDB and the regular Log file.

Mongo Log

Please get in touch with me if you have questions or comments.