How can I log the entire request body in WebAPI?

Vince

Posted on
May 24 2015

Is there a way to log the entire request body? Right now I'm transferring a few hundred kilobytes of base64 encoded images and the logged response which is viewable in the dashboard is cut short. I assume it's because the body is too large.

In order to debug correctly I need to see the entire response so I can try and reproduce it locally on my machine.

As a temporary workaround I've added a Handler but when I do this Raygun no longer logs errors...I can only assume it's because I've read the body already. Any help would be appreciated.

public class LogRequestAndResponseHandler : DelegatingHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            //logging request body
            string requestBody = request.Content == null
                ? "No request content avilable"
                : await request.Content.ReadAsStringAsync();

            //let other handlers process the request
            return await base.SendAsync(request, cancellationToken)
                .ContinueWith(task =>
                {
                    //once response is ready, log it
                    var responseBody = task.Result.Content == null 
                        ? "No response content available"
                        : task.Result.Content.ReadAsStringAsync().Result;

                    // Log request and response

                    return task.Result;
                });
        }
    }

Thanks.


John-Daniel Trask

Raygun

Posted on
May 25 2015

Hi Vince,

I suspect you're running up against the 100KB limit per message size. We do provide capacity for larger messages on enterprise plans (as you can imagine, even at 100KB, that's a maximum storage of 25GB on a Medium plan, per month, so a total of 175GB of data on a Medium plan for six months standard retention).

I'd recommend trying to trim the message size somewhat, or if you need to store a lot of data, simply attaching a URL that would work for your local store of it in the custom data. This would allow you to retain vastly more data in the response than our standard accounts support, but still quickly link to it from your Raygun account.

If you're interested in a larger plan offering, please don't hesitate to contact our sales team here: https://raygun.io/about/contact

Kind regards,

John-Daniel Trask


Vince

Posted on
May 26 2015

Thanks for your response.

Is there a reason why my custom handler is preventing Raygun from logging? I'm quite happy to log my response bodies locally on my side, but there's something about my implementation which affects Raygun.

Vince


Jamie Penney

Posted on
May 26 2015

It looks like you're capturing the request body in the same way as us (see here: https://github.com/MindscapeHQ/raygun4net/blob/master/Mindscape.Raygun4Net.WebApi/RaygunWebApiDelegatingHandler.cs) although I should probably add some more null checks around request.Content.

We're just adding the delegating handler into the MessageHandlers collection https://github.com/MindscapeHQ/raygun4net/blob/master/Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs#L96 - I wouldn't have thought there was anything special about that part.

Are you able to create a small project that reproduces this?

Cheers, Jamie


Vince

Posted on
May 26 2015

It seems plausible that others might add there own handlers after adding Raygun without realising it disables Raygun in the process.

Let me see if I can find the time :) I'll see if I can get this done today.


Vince

Posted on
May 27 2015

Hi,

My apologies but I don't have time to investigate this bug.

I strongly suggest that you investigate and this problem for yourself. It's in the best interest of your product.

Just start a new WebAPI project Add Raygun Throw an exception See it logged in Raygun

Add the trace listener example I posted Throw an exception No exception appears in Raygun.


Callum

Posted on
May 27 2015

Thanks for that repro - just letting you know that we are still looking into this. Will let you know the results soon.


Reply