A simple debug logging strategy for Azure web applications

Posted: (EET/GMT+2)

 

When hosting your own web servers and running your ASP.NET web applications there, you have the whole operating system and usually the hardware at your disposal. However, this is not the case when running your web applications on Azure, especially as web sites or web applications without a virtual machine.

Thus, you do not for instance have broad access to the file system, which is my default choice for writing development time debugging logs about the runtime operation of an application. You could write to the folder where your application is, say, the App_Data folder, but the problem is that once you deploy a new version from Visual Studio to Azure, those changes are (by default) lost.

When building a web application, I usually include debug logging so that I can confirm the application works at runtime the way I assume it to work. Since I separate debug logging from traditional operation logging (which usually goes to a database), I've come up with a pretty simple strategy for debug logging: use an in-memory storage, and create a simple view page for displaying the log at runtime.

More specifically, in an ASP.NET MVC application, I have a class named "Logging", which contains an internal method called "LogDebugMessage", which takes a string and stores that to the in-memory list of log entries along with a timestamp, thread id and calling method.

The message is then stored into an ConcurrencyBag class and can later be retrieved from it for display. Using the ConcurrencyBag class instead of a List class makes sure multiple requests of tasks don't confuse the class.

Hope this helps!