Closed Highbrainer closed 4 years ago
I see some issues related to the Response availability, what do you think @rdeago?
Hello @Highbrainer, thanks for using EmbedIO!
OnAfterHandler
is usually not needed: just implement IDisposable
in your controller and the Dispose
method will be called automatically Of course, as @geoperez pointed out, you will have no access to the HTTP context in a Dispose
method, but you can do something like this:
using System;
using System.Diagnostics;
using System.Net;
using EmbedIO.WebApi;
namespace YOUR.NAMESPACE
{
public sealed class TimingController : WebApiController, IDisposable
{
private static Stopwatch GlobalTimeBase = Stopwatch.StartNew();
private IPAddress _clientAddress;
private string _requestedPath;
private long _startTime = GlobalTimeBase.ElapsedMilliseconds;
protected override void OnBeforeHandler()
{
_clientAddress = HttpContext.RemoteEndPoint.Address;
_requestedPath = HttpContext.RequestedPath;
}
public void Dispose() => LogRequestTime(_clientAddress, _requestedPath, GlobalTimeBase.ElapsedMilliseconds - _startTime);
private void LogRequestTime(IPAddress clientAddress, string requestedPath, long elapsedTime)
{
// Your code here.
}
}
}
Examining EmbedIO's log output may also be of help, as the time needed to process each request is always logged after sending the response. Note, however, that the logged time includes the time spent sending the response to the client.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I have found WebApiController.OnBeforeHandler() method. It is very handy. I use it to perform common actions at the beginning of every valid incoming request.
Is there any possibility that you add its analog OnAfterHandler(), which would be called after the completion (either success or failure) of every valid incoming request ?
It would help me measure the time consumed by each request processing and optimize my code.
Thanks in advance !