Skip to main content

Logging on external storages.... lesson learns

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:

  • SQL instance
  • Azure Blob Storage
  • Azure Event Hub

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?
  • What should you do with extra data?

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.

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(

Azure AD and AWS Cognito side-by-side

In the last few weeks, I was involved in multiple opportunities on Microsoft Azure and Amazon, where we had to analyse AWS Cognito, Azure AD and other solutions that are available on the market. I decided to consolidate in one post all features and differences that I identified for both of them that we should need to take into account. Take into account that Azure AD is an identity and access management services well integrated with Microsoft stack. In comparison, AWS Cognito is just a user sign-up, sign-in and access control and nothing more. The focus is not on the main features, is more on small things that can make a difference when you want to decide where we want to store and manage our users.  This information might be useful in the future when we need to decide where we want to keep and manage our users.  Feature Azure AD (B2C, B2C) AWS Cognito Access token lifetime Default 1h – the value is configurable 1h – cannot be modified

What to do when you hit the throughput limits of Azure Storage (Blobs)

In this post we will talk about how we can detect when we hit a throughput limit of Azure Storage and what we can do in that moment. Context If we take a look on Scalability Targets of Azure Storage ( https://azure.microsoft.com/en-us/documentation/articles/storage-scalability-targets/ ) we will observe that the limits are prety high. But, based on our business logic we can end up at this limits. If you create a system that is hitted by a high number of device, you can hit easily the total number of requests rate that can be done on a Storage Account. This limits on Azure is 20.000 IOPS (entities or messages per second) where (and this is very important) the size of the request is 1KB. Normally, if you make a load tests where 20.000 clients will hit different blobs storages from the same Azure Storage Account, this limits can be reached. How we can detect this problem? From client, we can detect that this limits was reached based on the HTTP error code that is returned by HTTP