How to time a request to response lifetime?

≯℡__Kan透↙ 提交于 2019-12-10 17:56:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!