Мне нужно измерить время, необходимое для обработки каждого запроса в приложении-функции Azure, как показано ниже через 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;
}
}
}