实时监控Cat之旅~对Get和Post进行封装,支持分布式消息树
生活随笔
收集整理的這篇文章主要介紹了
实时监控Cat之旅~对Get和Post进行封装,支持分布式消息树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
對第三方接口的調用我們需要對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的監控,敬請期待!
轉載于:https://www.cnblogs.com/lori/p/5316343.html
總結
以上是生活随笔為你收集整理的实时监控Cat之旅~对Get和Post进行封装,支持分布式消息树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内存过高排查方法
- 下一篇: JS过滤空格,回车符。