码迷,mamicode.com
首页 > Windows程序 > 详细

(C#)日志接口请求响应时间

时间:2017-05-25 19:00:35      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:选择   .json   record   switch   ati   log   cti   标识   basedir   

日志接口响应时间,记录接口请求信息,响应结果以及响应时间等。可以清楚的分析和了解接口现在。

如果一个一个地在接口下面做日志,那不是我们想要的结果。所以,我们选择做一个特性来控制接口要不要记录请求响应日志。

关键代码如下:

public class LogRequestAndResponseAttribute: ActionFilterAttribute
{
private static string RequestLogSwitch = string.IsNullOrEmpty(ConfigurationSettings.AppSettings["RequestLogSwitch"])?"0": ConfigurationSettings.AppSettings["RequestLogSwitch"];//请求日志开关
/// <summary>
/// 重写请求开始
/// </summary>
/// <param name="actionContext"></param>
public override void OnActionExecuting(HttpActionContext actionContext)
{
if(RequestLogSwitch.Trim()!="0")
{
try
{
RequestRecord item = new RequestRecord();
string reqArguments = string.Empty;//请求参数
if (actionContext.Request.Method.Method == "GET")
{
reqArguments = actionContext.Request.RequestUri.Query;
}
else if (actionContext.Request.Method.Method == "POST")
{
reqArguments =(actionContext.ActionArguments!=null&& actionContext.ActionArguments.Count > 0) ? actionContext.ActionArguments.JSONSerializeV3() : string.Empty;
}
item.RequestId = actionContext.Request.GetCorrelationId();//请求唯一标识
item.Type = 0;
item.BaseUrl = actionContext.Request.RequestUri.AbsolutePath;//请求地址
item.RequestMethod = actionContext.Request.Method.Method;//请求方式
item.ReqArguments = reqArguments;
item.BeginRequestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
string jsonStr = item.JSONSerializeV3();
var path = @"{0}Log\LogRequest\{1}\log.txt".FormatStr(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
StaticFunctions.WriteStringToFile(path, jsonStr, true, Encoding.UTF8);
}
catch(Exception ex) {
var path = @"{0}Log\LogRequestErr\{1}\log.txt".FormatStr(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
StaticFunctions.WriteStringToFile(path, "{0}==>发生异常了,异常信息为:{1}\r\n,异常描述为:{2}\r\n,异常位置:{3}\r\n".FormatStr(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message, ex.InnerException.Message, ex.StackTrace), true, Encoding.UTF8);
}

}

base.OnActionExecuting(actionContext);
}

/// <summary>
/// 重写请求结束
/// </summary>
/// <param name="actionExecutedContext"></param>
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (RequestLogSwitch.Trim() != "0")
{
string jsonStr = string.Empty;
try
{
RequestRecord item = new RequestRecord();
item.Type = 1;
item.RequestId = actionExecutedContext.Request.GetCorrelationId();
item.EndRequestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
item.ResponseCode = actionExecutedContext.Response.StatusCode.GetInt();
jsonStr = item.JSONSerializeV3();
var path = @"{0}Log\LogRequest\{1}\log.txt".FormatStr(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
StaticFunctions.WriteStringToFile(path, jsonStr, true, Encoding.UTF8);
}
catch(Exception ex)
{
var path = @"{0}Log\LogRequestErr\{1}\log.txt".FormatStr(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
StaticFunctions.WriteStringToFile(path, "{0}==>发生异常了,异常信息为:{1}\r\n,异常描述为:{2}\r\n,异常位置:{3}\r\n".FormatStr(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),ex.Message,ex.InnerException.Message,ex.StackTrace), true, Encoding.UTF8);
}

}

base.OnActionExecuted(actionExecutedContext);
}

}

(C#)日志接口请求响应时间

标签:选择   .json   record   switch   ati   log   cti   标识   basedir   

原文地址:http://www.cnblogs.com/hanisem151/p/6905283.html

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