HTTP Client Errors

This feature, once enabled, automatically captures HTTP client errors, like bad response codes, as error events and reports them to Sentry.

Once enabled, this feature automatically captures HTTP client errors, like bad response codes, as error events and reports them to Sentry. The error event will contain information from the HTTP request and response, such as url, status_code, and so on.

To enable it:

Copied
// Add this to the SDK initialization callback
options.CaptureFailedRequests = true;

HTTP client errors will then be captured for any requests from the Sentry SDK. To capture HTTP client errors for outbound requests from your own application, pass SentryHttpMessageHandler as the inner handler when creating your HttpClient:

Copied
using var httpClient = new HttpClient(new SentryHttpMessageHandler());

By default, only HTTP client errors with a response code between 500 and 599 are captured as error events, but you can change this behavior by modifying the FailedRequestStatusCodes option:

Copied
options.FailedRequestStatusCodes.Add((400, 499));

HTTP client errors from every target (.* regular expression) are automatically captured, but you can change this behavior by updating the FailedRequestTargets option with either regular expressions or plain strings. A plain string must contain at least one of the items from the list. Plain strings don't have to be full matches, meaning the URL of a request is matched when it contains a substring provided through the option:

Copied
options.FailedRequestTargets.Add("foo");      // substring
options.FailedRequestTargets.Add("foo.*bar"); // regex

When the SendDefaultPii option is enabled, error events may contain PII data such as Headers and Cookies. Sentry already does data scrubbing by default, but you can also scrub any data before it is sent. Learn more in Scrubbing Sensitive Data.

These events are searchable and you can set alerts on them if you use the http.url and http.status_code properties. Learn more in our full Searchable Properties documentation.

The captured error event can be customized or dropped with a beforeSend:

Copied
options.SetBeforeSend((@event, hint) =>
{
    if (hint.Items.TryGetValue(HintTypes.HttpResponseMessage, out var responseHint))
    {
        var response = (HttpResponseMessage)responseHint!;
        var request = response.RequestMessage!;

        // Do something with the request and/or response... for example, you could add
        // some custom context to the event for specific scenarios
        var statusCode = response.StatusCode;
        if (statusCode == HttpStatusCode.Unauthorized)
        {
            @event.Contexts["Unauthorized"] = request.RequestUri.GetComponents(
                UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped
                );
        }
    }

    // return the modified event
    return @event;
});
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").