Performance logging with Client

We are using JsonServiceClient in our xamarin.forms apps.
It work very well by the way :slight_smile:

Our application is having some performance issues especially when a user is over the Atlantique (Canada vs France).
We spot some issues in our code but now I need more data to see where is the issue. The worst case we had was 3 minutes difference. I was on the phone with the user and were accessing the same info.

Do we have a switch that we can enable in the client that I could Log the start and end of the query locally on the IOS or Android device. The log could be 2 lines, Line 1 start time, Line 2 End time with the size of the data transfert.

Where can I add this kind of functionality in the client what method I can override ?
Any suggestion are welcome.


1 Like

You can use Global Request/Response Filters to execute custom logic before and after requests are sent and received:

// Executed for all .NET HttpWebRequest ServiceClient instances like JsonServiceClient:
ServiceClientBase.GlobalRequestFilter = (HttpWebRequest req) => { ... };
ServiceClientBase.GlobalResponseFilter = (HttpWebResponse res) => { ... };

// Executed for all JsonHttpClient instances
JsonHttpClient.GlobalRequestFilter = (HttpRequestMessage req) => { ... };
JsonHttpClient.GlobalResponseFilter = (HttpResponseMessage res) => { ... };

Or you can use instance Request/Response Filters if you only want to run custom logic for a specific instances:

var client = new JsonServiceClient(baseUrl) {
    RequestFilter = req => { ... },
    ResponseFilter = res => { ... },

var client = new JsonHttpClient(baseUrl) {
    RequestFilter = req => { ... },
    ResponseFilter = res => { ... },
1 Like

There is alway an easy way !! :smile:

Thanks a lot!

res.ContentLength is always -1, is their a way to get the size of the DTO or the total size of the response including the DTO.

It returns -1 when the Content-Length is unknown by the server, e.g. when returning streaming chunked responses.

There’s no way to determine the length other than reading the entire stream and keeping a count of bytes read, which is something the Service Clients doesn’t do as the Stream is deserialized by the serializer.

1 Like

We do byte accounting for our api to get size of response DTO as such (not CPU or memory efficient but only way we’ve found…)

 GlobalResponseFilters.Add((httpReq, res, responseDto) =>


                var dto = responseDto is IHttpResult httpResult ? httpResult.Response : responseDto;

                var bytesUsed = ServiceStack.Text.JsonSerializer.SerializeToString(dto).Length;

1 Like