Instrumenting Web APIs for requests enlisted the sync process

To round out the synchronisation processes logging I needed to instrument each Web API called made that formed part of a sync.

This was done in one of two ways. It was either triggered by the client by sending a SyncId in the header or if a SyncId value was present in the users session.

NOTE: its important this method returns a response because if you don’t the Web API response might be a 404 “Not Found” which happened in my first implementation after a logical error in my error handling (stripped from the below example for simplicity).


public class SyncLogHandler : DelegatingHandler
    protected async override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        DateTime startTimestamp = DateTime.Now;
        string clientSyncModel = string.Empty;

        int clientSyncId = SyncHelper.GetSyncIdFromRequest(request);

        if ((clientSyncId != 0) && (request.RequestUri.ToString().Contains("login") == false))
            var requestContentByteArray = await request.Content.ReadAsByteArrayAsync();
            clientSyncModel = Encoding.UTF8.GetString(requestContentByteArray);

        // Call the rest of the pipeline of work 
        var response = await base.SendAsync(request, cancellationToken);

        // We still call the sync service in case the server believes we are still in sync mode.
        var syncService = request.GetDependencyScope().GetService(typeof(ISyncService)) as ISyncService;

        return response;


This is part of a 3 part series on aspects of implementing a synchronisation process with throttling capabilities:

  1. Architecting a data synchronisation process between SAP and multiple internal mobile applications
  2. Instrumenting Web APIs for requests enlisted the sync process
  3. Architecting a Throttling Mechanism