Issue 41

Logging on external storages.... lesson learns

Radu Vunvulea
Solution Architect


Logging and audit is a must have to for all applications. Without this information, monitoring and support team would not be able to know what is happening in the system, if the system works correctly and what happens in a specific point in time.

On top of this, from a security perspective, you need to audit at different levels of your system who is accessing your system, what is the action and when.

There are many solutions out of the box on the market that help us to do logging and audit in our system. I suppose that all of us used at least one time in their life log4net or NLog. There are situations when you need to persistent logs in storage that are not on the same machine where your system runs. For example, a common use case can be writing all this information to:

But, did you ask yourself what is happening when this storage cannot be reached. This post will cover this case, what if … the storage where I persist logs and audit cannot be reached.

Let's imagine a system that write all the logs to Azure Blob Storage and audit information are send directly to Azure Event Hub, from where are analyzed in real time to detect any security problems or instability of the system.

To reduce the network load, improve performance (speed) and control costs, each component has a buffer of where logs and audit data are written. Once the buffer reached a specify size, the content is flushed automatically. This would work perfect as long as Azure Blob Storage (for logs) and Azure Event Hub (for audit) are available.

Remarks: We will go forward with the case when you are using log4net, but similar behavior will exist with other logging frameworks. 

What is happening when one of this storage cannot be reach?

What do you think?

The buffer will become bigger and bigger. Normally, this buffer in memory because you want to have low latency for write operations.

You will start to consume more and more memory and there is a high probability to end up with an out of memory exception, that will not only will block your component or application, but also will cause you to lose the current logs and audit data.

Losing this data will not help you too much when you will need to trace what happen, why the component (application) is not working or why logs and audit data are not persistent.

Another thing that we need to take into account is that when you write to a different destination that the default one (especially external locations), you need to think at this situation and how you should handle them.

What should I do?

There three important actions that you should make:

Event Log

You should ensure that all exception or odd behaviors that appears at logger level are written in Event Logger. Writing logger exceptions in this location will guaranty you that you can trace and identify any kind of issues with your logging mechanism.

Another possibility is to write directly on disk, as files, but Event Logger is a very powerful tool and monitoring and support team can aggregate automatically all event logs, define alerts over them and so on.

This should be your last safetymen when your logging system is not working as expected. Don't forget to think about where to log data from the moment when your component (application) start until in the moment when your logging component is initialized - what happens if it fails and how you can detect this. This are two questions that you should ask yourself.

Temporary Storage

In the moment when the logging mechanism wants to make a flush but detects that the remote storage cannot be reached a custom action should be triggered.

A possibility is to have a retry mechanism that would retry to flush the data, but:

For how long you should retry? There is no an out of the box response. I highly recommend to try only for a few time and after that you a backup solution. For example I would make 3 retries (1s, 2s and 4s). If the logging or audit storage would be still down that I would go on the backup solution (the solution will be presented below).

What should you do with extra data?

You cannot store data in the buffer, because your buffer is already full. It might be possible to do this for a few second or minutes, but this is not feasible for 1h or 4h. In this situation you should flush all the data that you have in the buffer on your local disk.

In this way you will clear your flush and the new logs and audit data can be persisted without having issues with out of memory exception use case. On top of this you should ensure, that once you succeed to write logs or audit on the external storage system (in our case was blob and event hub), you should write all data that was stored on the local storage to external storage.

With this solution, you will need to take into account the case when the external storage is down for a long period of time and you will be run out of space on your local storage. A cleanup mechanism should be in place for this situations. The simplest solution, that can be implement with success is for all logs or audit file older than X hours or days delete them.

Using such a cleanup mechanism, local space needed for your system can be forecast easily and special cases when logs and audit files are using the available space that should be used by other system that run on the same machine will not exist.

Passive Storage

In comparison with previous solutions, this is an optional solution, that should be used only when is critical to receive logs or audit data in a specific time interval. This solution will come with extra costs and can add also a little complexity on the system that process and analyze logs and audit trails.

This solution doesn't exclude the previous ones because both active and passive storage can go down of course. 

This solution involve using two different storages, one active that is used all the time and a secondary one (passive) that is used only when the first one is not available.

In conclusion I highly recommend to put on the table on this case for all your system. At least write in the Event Logger any errors or strange behavior of your logging mechanism.




  • Accenture
  • BT Code Crafters
  • Accesa
  • Bosch
  • Betfair
  • MHP
  • BoatyardX
  • .msg systems
  • Yardi
  • P3 group
  • Ing Hubs
  • Colors in projects