如何将请求计时到响应生命周期? [英] How to time a request to response lifetime?

查看:39
本文介绍了如何将请求计时到响应生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在向请求对象添加一个 cookie,并从响应中的当前时间中减去它,但我假设有更好的方法来做到这一点.此外,在实际用例中可能不会重复使用相同的 Cookie 密钥.

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.

有什么想法吗?

            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();
                                });

推荐答案

不,不要弄乱请求 cookie.一种可能的方法是将日期时间添加到请求过滤器中的请求上下文中.

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.

您或许可以使用 ServiceStacks 内置分析

这篇关于如何将请求计时到响应生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆