问题
I'm currently adding a cookie to the request object, and subtracting it from the current time in the response, but I'm assuming there's a better way to do this. Also, re-using the same Cookie key probably won't do in real use case.
Any ideas?
RequestFilters.Add((request, response, dto) =>
{
request.Cookies.Add("Start", new Cookie("Start", DateTime.Now.ToString()));
request.ResponseContentType = ContentType.Json;
});
ResponseFilters.Add((request, response, dto) =>
{
Console.WriteLine("{0} : [{1}]", request.RawUrl, DateTime.Now.Subtract(DateTime.Parse(request.Cookies["Start"].Value)).Milliseconds);
request.Cookies.Clear();
});
回答1:
No, dont mess with the request cookies. One possible way is to just add the datetime to the request context in the request filter.
request.Items.Add("RequestTimer", DateTime.Now);
and then read it out in the response filter
DateTime.Now.Subtract((DateTime) request.Items["RequestTimer"]);
But the accurancy will depend on what order the request/response filters are run.
You might be able to utilize ServiceStacks built-in profiling instead
来源:https://stackoverflow.com/questions/18540687/how-to-time-a-request-to-response-lifetime