【问题标题】:Measure request processing time with Azure Function App使用 Azure Function App 测量请求处理时间
【发布时间】:2019-05-13 13:56:51
【问题描述】:

我需要测量在 Azure Function App 中处理每个请求所花费的时间,如下面通过 ASP.NET(非 ASP.NET CORE)

public class RequestLogHandler : DelegatingHandler
    {
        private readonly ILogger _logger;

        public RequestLogHandler(ILogger logger)
        {
            _logger = logger;
        }

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            string path = request.GetOwinContext().Request.Path.ToString();
            string method = request.Method.Method;

            var sw = Stopwatch.StartNew();

            var response = await base.SendAsync(request, cancellationToken);

            sw.Stop();

            int statusCode = (int)response.StatusCode;

            _logger.HttpRequest(path, method, sw.ElapsedMilliseconds.ToString(), statusCode.ToString());

            return response;
        }
    }

设置

 public class WebServer : IWebServer
    {
        private readonly ILogger _logger;
        private readonly HttpConfiguration _httpConfiguration;

        private IDisposable _server;

        public WebServer(ILogger logger, HttpConfiguration httpConfiguration)
        {
            _logger = logger;
            _httpConfiguration = httpConfiguration;
        }

        public void Start(string url)
        {
            _server = WebApp.Start(url, (appBuilder) =>
            {
                _httpConfiguration.MapHttpAttributeRoutes();
                _httpConfiguration.MessageHandlers.Add(new RequestLogHandler(_logger));

                appBuilder.UseWebApi(_httpConfiguration);
            });
        }

        public void Dispose()
        {
            if(_server != null)
            {
                _server.Dispose();
                _server = null;
            }
        }
    }

【问题讨论】:

    标签: c# asp.net-core .net-core azure-functions asp.net-core-webapi


    【解决方案1】:

    您可以使用Application Insights 获取请求持续时间,在应用洞察页面中您可以获取请求持续时间的详细信息。

    这是Duration description:此字段是必需的,因为请求遥测表示具有开头和结尾的操作。您还可以使用 Microsoft.ApplicationInsights NuGet 包来send custom telemetry data 到 Application Insights,包括 Duration。

    【讨论】:

    • 感谢您的回复。但这并不是我所要求的。我可以从 App Insights 中看到,这很好。但我一直在寻找编码,它测量请求管道中整个请求的时间。此逻辑可以测量所有请求。遥测,我必须为每个功能编码,它不会测量整个请求。
    猜你喜欢
    • 1970-01-01
    • 2019-05-28
    • 2021-01-26
    • 2018-12-07
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多