RSS

Tag Archives: WebAPI

Log request URI in ASP.NET Web API

If you want to know how your users call your ASP.NET Web API you can hook into the process by implementing a custom Message Handler. This is done by creating a class that inherits from DelegatingHandler. In the overwritten SendAsync method you can implement your logging structure:

public class UrlCallLogger : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(
                                HttpRequestMessage request,
                                CancellationToken cancellationToken)
    {
        Debug.WriteLine(request.RequestUri);
        // do more with your logging
        // [...]
        return base.SendAsync(request, cancellationToken);
    }
}

Then in the Register method of the WebApiConfig class you have to add following line:

config.MessageHandlers.Add(new UrlCallLogger());

About how to config

Advertisements
 
Leave a comment

Posted by on May 26, 2015 in C-Sharp

 

Tags: