[ACCEPTED]-Web API response time-asp.net-web-api
Accepted answer
If you want to implement Web Api Monitoring, you 6 can create a custom DelegatingHandler to 5 track action duration and status.
Here is 4 a very basic example to measure operation 3 duration. The duration is added to response 2 (quite useless) ; it's better to store this 1 kind of data to a dedicated repository.
public class MonitoringDelegate : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var watcher = Stopwatch.StartNew();
var response = await base.SendAsync(request, cancellationToken);
watcher.Stop();
//store duration somewheren here in the response header
response.Headers.Add("X-Duration", watcher.ElapsedMilliseconds.ToString());
return response;
}
}
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.