Store logs into Azure Blob Storage

In this article I will show you how to write logs in Blob table. As log manager in the system I will use Log4net because Blob gives us a great possibility to work with this Log4net. 

Log4Net is a popular logging framework, and if you have an existing application that you wish to move to Azure computer, you probably want to avoid rewriting your application to use another logging framework. Luckily, keeping Log4Net as your logging tool in Azure is certainly possible, but there are a few hoops you have to jump through to get there. So we will write our logs from project to some blob table. For this purpose we can use our project from the previous topic that already have a connection to Azure and to Blob Storage account. So let's start.


In very first step we need to add the Log4net library to our project. We can use NuGet for this purpose. So we should open our MvcApplicationWebRole project and right click on folder References and select “Manage NuGet Packages”. NuGet will occur.  Enter “log4net” in the search window. And press install when it find this library:

Add Log4net library to project - Manage NuGet Packages

Add Log4net library to project


In next step we need to create log entity. It should inherit from base class TableServiceEntity. In result – it will create the table in our storage account with some default columns and with columns as property from our entity You can create this class as you want and add properties you need. I will show my entity:

Azure Blob Storage - Log entity

Log entity


In next step we should create appender – class that will use our entity and add it to blob table every time when in the system will run code with write log:

Azure Blob Storage - Log appender

Log appender


Next we should add log4net configuration in method Application_Start():

Log4net configuration in Global.asax in Azure

Log4net configuration in Global.asax


In the final step we should add some configuration in Web.config in section “configuration”. We need to configure new section “log4net” and add in the keys future table name:

Web.config configuration in Azure

Web.config configuration


Now let's add some log in system:

Add log in BlobDocuments controller Azure Blob Storage

Add log in BlobDocuments controller


Now we can run it and check blob via Cloud Storage Studio. But in very first time we should run the code with some log twice. Because in the first time Log4NetAppender will throw an Exception “DataServiceRequestException” because blob table still does not exist. In all other cases it will exist so we will lose just first log. Not a big problem. So – let's run our solution, go to “AllDocuments” view and check our blob storage account:

New table and log in Azure Blob Storage

New table and log in Blob Storage


So from the above image we can see the new table in section “Tables” and we can see our first log with message “Get 3 documents from Blob”.


Best Regards. Andrew Tsopych CTO of Diceus