前段时间在开发一个协议站点供客户端(Android/IOS)使用,因业务需要统计各协议的调用频率。将记录以日志的形式记录在日志系统中。 简单分析了一下,技术方案大致分为两种: 方案A:每个业务模块需要埋点的协议单独埋点。 方案B:封装一个HttpModule。记录所有 ...
前段时间在开发一个协议站点供客户端(Android/IOS)使用,因业务需要统计各协议的调用频率。将记录以日志的形式记录在日志系统中。
简单分析了一下,技术方案大致分为两种:
方案A:每个业务模块需要埋点的协议单独埋点。
方案B:封装一个HttpModule。记录所有的请求。
方案A与方案B的优缺点就不在分析了。在我们的项目有两个个小组做类似的协议站点,我们采用的是方案B。而另外的一个小组采用方案A。
在此我重点说一下采用的方案B的理由:
1、麻烦一个人,一个人开发完毕后,其他人不再去花时间增加记录。统一记录。
2、方便排错,还可以做请求参数与输出参数的记录,也不用在业务中记录返回的值。业务开发只关心业务即可。记录业务中的常规日志即可。
代码比较简单。与协议之间走的是Post请求发送Json串。请求串全在Request.InputStream中。两个文件一个处理请求(Handle.cs),一个获取数据(CatchTextStream.cs)
Handle.cs
using System;using System.IO;using System.Web;using Logs;namespace HttpModule.Api{ public class Handle : IHttpModule { private get='_blank'>string json = string.Empty; private string requestItemKey = "request.json"; private string responseItemKey = "response.json"; void IHttpModule.Dispose() { } void IHttpModule.Init(HttpApplication context) { context.BeginRequest += new EventHandler(beginRequest); context.EndRequest += new EventHandler(endRequest); } private void beginRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; var context = application.Context; if (context == null) { return; } //排除不需要的链接 if (context.Request.InputStream.Length > 0) { context.Response.Filter = new CatchTextStream(context.Response.Filter, responseItemKey); context.Request.InputStream.Position = 0; //设置流的位置 StreamReader reader = new StreamReader(context.Request.InputStream); //request请求流 string json = reader.ReadToEnd(); context.Request.InputStream.Seek(0, SeekOrigin.End);//重置流读取位置,如果不重置在方法中无法获取json串 context.Request.RequestContext.HttpContext.Items[requestItemKey] = json; } else//只记录请求参数 { _log.Info("请求url:" + context.Request.Url); } } private void endRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; var context = application.Context; if (context.Request.RequestContext.HttpContext.Items.Contains(requestItemKey)) { string requestJson = context.Request.RequestContext.HttpContext.Items[requestItemKey].ToString(); string responseJson = context.Request.RequestContext.HttpContext.Items[responseItemKey].ToString(); //当前请求状态 返回值中带code:0表示协议请求成功 status=日志系统使用标识,1=请求正常返回,0=请求异常 int status = responseJson.Contains("\"code\":\"0\"") ? 1 : 0; writeLog(context.Request.Url.LocalPath, requestJson, "return=" + responseJson, status); } } private void writeLog(string url, string parms, string msg, int status) { var entity = new Logs.LogRecord("站点名称", LogLevelType.Monitor, url) { ParasNameValue = parms, ErrMessage = msg, Status = status }; Logs.LogNetHelper.WriteLog(entity); } }}
原标题:web api 记录请求
关键词:web
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。