我正在构建一个服务器监视系统,并希望向Web发送一个请求,并在JSON对象中获取服务器的健康状况,如果它正常,如果它的数据库连接正常,那么它的响应时间等等。
我如何实现响应时间,说出Web API响应请求所需的时间?
发布于 2014-05-18 17:30:53
你可以在你的客户上启动一个秒表,当你提升你的客户时,你可以停止它。
发布于 2014-05-19 08:18:22
如果要实现Web监视,可以创建一个自定义DelegatingHandler来跟踪操作持续时间和状态。
这里是一个非常基本的例子来测量操作持续时间。持续时间被添加到响应中(非常无用);最好将此类数据存储到专用存储库中。
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;
}
}https://stackoverflow.com/questions/23724844
复制相似问题