【问题标题】:How To Get Elapsed Time Getting REST GET request in async / await with httpclient in .net core如何获取经过时间在.net核心中使用httpclient异步获取REST GET请求/等待
【发布时间】:2019-05-03 04:02:18
【问题描述】:

当我用 Task.Wait 调用下面的方法 DoRestCall 时,经过的时间基本上显示为 0(好吧,它显示 3ms)。我知道这是因为异步/等待模式。我想获得完成其余通话所需的实际时间。我想不出办法进入 HttpClient 来解决这个问题。

public static async Task DoRestCall()
{
    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync("https://http://myrest.com");
    if (response.IsSuccessStatusCode)
    {
        var sw = new Stopwatch();
        sw.Start();
        var x = await response.Content.ReadAsStringAsync();
        sw.Stop();
        Console.WriteLine($"{x.Length} {sw.ElapsedMilliseconds}");
    }
}

【问题讨论】:

  • 为什么只在请求后才启动秒表?您正在计时 ReadAsString 部分。异步不是这里的问题,只是秒表的位置。

标签: c# asp.net-core .net-core async-await


【解决方案1】:

您应该移动秒表以将您的 .GetAsync 呼叫也包括在内。

public static async Task DoRestCall()
{
    HttpClient client = new HttpClient();
    string responseBody = String.Empty;
    var sw = new Stopwatch();
    sw.Start();
    HttpResponseMessage response = await client.GetAsync("https://http://myrest.com");
    if (response.IsSuccessStatusCode)
    {        
        responseBody = await response.Content.ReadAsStringAsync();
    }
    sw.Stop();    
    Console.WriteLine($"{responseBody.Length} {sw.ElapsedMilliseconds}");
}

【讨论】:

    【解决方案2】:

    其他方式查看Elapsed Time的HttpClient

    1.将Log Level 改为InformationTrace

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          //"System.Net.Http.HttpClient": "Information"
        }
      }
    }
    

    样本输出

    info: System.Net.Http.HttpClient.MyClient.LogicalHandler[100]
      Start processing HTTP request GET https://api.github.com/repos/aspnet/docs/branches
    
    info: System.Net.Http.HttpClient.MyClient.ClientHandler[100]
      Sending HTTP request GET https://api.github.com/repos/aspnet/docs/branches
    
    info: System.Net.Http.HttpClient.MyClient.ClientHandler[101]
      Received HTTP response after 682.9818ms - OK
    
    info: System.Net.Http.HttpClient.MyClient.LogicalHandler[101]
      End processing HTTP request after 693.1094ms - OK
    

    2.创建一个新的Handler

    public class TimingHandler : DelegatingHandler
    {
        private readonly ILogger<TimingHandler> _logger;
    
        public TimingHandler(ILogger<TimingHandler> logger)
        {
            _logger = logger;
        }
    
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, 
            CancellationToken cancellationToken)
        {
            var sw = Stopwatch.StartNew();
    
            _logger.LogInformation("Starting request");
    
            var response = await base.SendAsync(request, cancellationToken);
    
            _logger.LogInformation($"Finished request in {sw.ElapsedMilliseconds}ms");
    
            return response;
        }
    }
    

    更多详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多