码迷,mamicode.com
首页 > 其他好文 > 详细

实时监控Cat之旅~对Get和Post进行封装,支持分布式消息树

时间:2016-03-24 18:18:00      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:

对第三方接口的调用我们需要对GET和POST进行监控,看一些请求的执行是否成功,如A调用B,B调用C,C调用D,这一连串的东西需要我们使用cat进行记录,进行记录之后,我们可以很容易的发现请求响应的时间及是否出错,下面是我对这两种请求的封装。

    /// <summary>
    /// cat中使用的HttpClient
    /// </summary>
    public class CatHttpClient
    {
        /// <summary>
        /// 返回当前Cat上下文
        /// </summary>
        /// <returns></returns>
        static CatContext GetCurrentContext(string message)
        {

            string currentUrl = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
            var context = PureCat.CatClient.GetCatContextFromServer();
            if (context == null)
            {
                context = PureCat.CatClient.DoTransaction("xuexiba", currentUrl, () =>
                {
                    PureCat.CatClient.LogEvent("xuexiba", message, "0", currentUrl);
                });

            }
            else
            {

                context = PureCat.CatClient.DoTransaction("xuexiba", currentUrl, () =>
                {
                    PureCat.CatClient.LogRemoteCallServer(context);
                    PureCat.CatClient.LogEvent("xuexiba", message, "0", currentUrl);
                });
            }
            return context;
        }
        /// <summary>
        /// Post数据
        /// </summary>
        /// <param name="requestUri"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static HttpResponseMessage Post(string requestUri, HttpContent content)
        {
            var handler = new HttpClientHandler() { };
            using (var http = new HttpClient(handler))
            {
                PureCat.CatClient.SetCatContextToServer(http, GetCurrentContext("Post Request Sent..."));
                var response = http.PostAsync(requestUri, content).Result;
                return response;
            }
        }
        /// <summary>
        /// Get数据
        /// </summary>
        /// <param name="requestUri"></param>
        /// <returns></returns>
        public static HttpResponseMessage Get(string requestUri)
        {
            var handler = new HttpClientHandler() { };
            using (var http = new HttpClient(handler))
            {
                PureCat.CatClient.SetCatContextToServer(http, GetCurrentContext("Get Request Sent..."));//设置接口api的头,发送
                var response = http.GetAsync(requestUri).Result;
                return response;
            }
        }
    }

在程序中使用非常方便,如下代码,一看便知

[AllowAnonymous]
        public void Step1()
        {
            Lind.DDD.CatClientPur.CatHttpClient.Get("Http://localhost:4829/AdminCommon/Step2");
        }

        [AllowAnonymous]
        public void Step2()
        {
            Lind.DDD.CatClientPur.CatHttpClient.Get("Http://localhost:4829/AdminCommon/Step3");
        }

        [AllowAnonymous]
        public void Step3()
        {
            Lind.DDD.CatClientPur.CatHttpClient.Get("Http://localhost:4829/AdminCommon/Step4");
        }
        [AllowAnonymous]
        public void Step4()
        {
            Lind.DDD.CatClientPur.CatHttpClient.Get("Http://localhost:4829/AdminCommon/Error");
        }

而它产生的消息树也是我们希望看到的,即从step1到step4的记录,如图

技术分享

下一讲我们将读一下,如何对一个请求从开始到结束进行cat的监控,敬请期待!

实时监控Cat之旅~对Get和Post进行封装,支持分布式消息树

标签:

原文地址:http://www.cnblogs.com/lori/p/5316343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!