如果一个一个地在接口下面做日志,那不是我们想要的结果。所以,我们选择做一个特性来控制接口要不要记录请求响应日志。
关键代码如下:
public class LogRequestAndResponseAttribute: ActionFilterAttribute
{
private static string RequestLogSwitch = string.IsNullOrEmpty(ConfigurationSettings.AppSettings["RequestLogSwitch"])?"0": ConfigurationSettings.AppSettings["RequestLogSwitch"];//请求日志开关
/// <summary>
/// 重写请求开始
/// </summary>
/// <param></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></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);
}