簡體   English   中英

Web API響應時間

[英]Web API response time

我正在構建服務器監視系統,並希望將請求發送到Web API,並通過JSON對象(如果數據庫連接正常,響應時間等)獲取服務器的運行狀況。

如何實現響應時間,說Web API回答請求需要多長時間?

如果要實施Web Api監視,則可以創建自定義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;
    }
}

您可以在客戶端啟動一個秒表 ,並在返回篷房時將其停止

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM