本文共 2920 字,大约阅读时间需要 9 分钟。
对第三方接口的调用我们需要对GET和POST进行监控,看一些请求的执行是否成功,如A调用B,B调用C,C调用D,这一连串的东西需要我们使用cat进行记录,进行记录之后,我们可以很容易的发现请求响应的时间及是否出错,下面是我对这两种请求的封装。
////// cat中使用的HttpClient /// public class CatHttpClient { ////// 返回当前Cat上下文 /// ///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; } /// /// Post数据 /// /// /// ///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; } } /// /// Get数据 /// /// ///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的监控,敬请期待!
本文转自博客园张占岭(仓储大叔)的博客,原文链接:,如需转载请自行联系原博主。