Skip to content

coderrio/coderr.client.aspnet.webapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ASP.NET WebApi

This documentation is for the ASP.NET WebApi library. You can find out other libraries here.

This library will report all unhandled ASP.NET WebApi exceptions. It can also report if something goes too slow or if there are too many authentication failures.

Prerequisites

You should have installed Coderr.Client.AspNet.WebApi.

Getting started

To activate the automated reporting, add this to your WebApiConfig in the App_Start folder.

// This is the base configuration for coderr.
// Change the URL if you are using Coderr Premise or Coderr Community.
var url = new Uri("https://report.coderr.io/");
Err.Configuration.Credentials(url,
    "yourAppKey",
    "yourSharedSecret");

// "config" should be the http configuration for WebApi.
Err.Configuration.CatchWebApiExceptions(config);

Once done, try to throw an exception in one of your WebApi controllers.

Reporting through the WebApi pipeline.

To make sure that Coderr can collect all information when manually handling exceptions, do not use Err.Report but instead use one of the following methods:

public string Get(int id)
{
    try
    {
        SomeMethod();
    }
    catch (Exception err)
    {
        // Uses the controller to pickup information.
        //
        // do note that normally you do not add try/catch in actions
        // since it's better to let exceptions flow through WebApi.
        this.ReportToCoderr(err);
    }

    return "value";
}

Another possibility when you only have access to the HttpRequestMessage.

internal class SomeMessageHandler : DelegatingHandler
{
    /// <inheritdoc />
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
        CancellationToken cancellationToken)
    {
        try
        {
            return await base.SendAsync(request, cancellationToken);
        }
        catch (Exception ex)
        {
            // Report it to Coderr
            request.ReportToCoderr(ex);
            
            return new HttpResponseMessage(HttpStatusCode.InternalServerError);
        }
    }
}

Tracking performance

Making sure that requests are processed quickly can be important. Coderr allows you to report slow requests with a simple configuration:

Err.Configuration.TrackPerformance(x =>
{
    // Everything under 500ms is ok.
    if (x.ExecutionTime.TotalMilliseconds < 500) return;

    // GET requests must be faster than 500ms.
    if (x.Request.Method == HttpMethod.Get)
    {
        x.ReportRequest();
    }

    // POSTs most be faster than 2 seconds.
    if (x.Request.Method == HttpMethod.Post && x.ExecutionTime.TotalMilliseconds > 2000)
    {
        x.ReportRequest();
    }
});

Your can of course set different requirements for different areas in your web site.

Example error message from Coderr:

Slow Request 'Values.Get' took 51970ms

Logging

Coderr will automatically include the 100 latest log entries from the ASP.NET WebApi tracing functionality. You can fine tune that using the ConfigureLogging method.

Err.Configuration.ConfigureLogging(options =>
{
    options.MaxAge = TimeSpan.FromMinutes(2);
    options.MinLevel = TraceLevel.Info;
    options.MaxEntries = 50;
});

What do I get?

The library can of course report unhandled exceptions, but also:

  • Slow HTTP requests
  • Invalid models
  • 401 and 403 responses

For every reported exception we will provide (when available):

  • Action information
  • Logged in user (username or tokenized) (i.e. allows you to track number of affected users)
  • Model state (if fields are OK or why they failed validation)
  • Request content
  • Request information
  • Request cookies
  • Route data

Examples

About

Automated exception handling for ASP.NET WebApi

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published