Web API响应时间

我正在构建一个服务器监视系统,并希望向Web API发送请求,并在JSON对象中获取服务器的运行状况,如果没有问题,如果数据库连接正常,响应时间等等。

如何实现响应时间,说明Web API响应请求需要多长时间?

你可以在你的客户端上启动一个秒表 ,并在你收回你的芒果时停止它

如果要实施Web Api Monitoring,可以创建自定义DelegatingHandler以跟踪操作持续时间和状态。

这是测量操作持续时间的一个非常基本的例子。 持续时间被添加到响应中(非常无用); 最好将这种数据存储到专用存储库中。

public class MonitoringDelegate : DelegatingHandler { protected override async Task 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; } }