1、API项目下创建MonitorApiAttribute

public class MonitorApiAttribute : ActionFilterAttribute
{
private static readonly string key = "enterTime"; public override void OnActionExecuted(HttpActionExecutedContext filterContext)
{
try
{
object beginTime = null;
if (filterContext.Request.Properties.TryGetValue(key, out beginTime))
{
var user = HttpContext.Current.User as UserInfo;//登录时储存的User
var indentify = user != null ? user.Id.ToString() : null; MonitorApiHelper.Monitor(beginTime, filterContext.Request, filterContext.Response, indentify);
}
}
catch (Exception)
{
} base.OnActionExecuted(filterContext);
} public override void OnActionExecuting(HttpActionContext actionContext)
{
actionContext.Request.Properties[key] = DateTime.Now.ToBinary(); base.OnActionExecuting(actionContext);
}
}

2、Common项目下创建MonitorApiHelper帮助类

public class MonitorApiHelper
{ public static async void Monitor(object beginTime, HttpRequestMessage httpRequestMessage, HttpResponseMessage httpResponseMessage, string indentify = "", string moduleId = "")
{
DateTime time = DateTime.FromBinary(Convert.ToInt64(beginTime));
var responseTime = Convert.ToInt32((DateTime.Now - time).TotalMilliseconds); var appId = "myapp";
var clientType = 0;
var responseStatus = 0; var request = HttpContext.Current.Request;
var browerVersion = request.UserAgent;
var clientIP = request.UserHostAddress;
var serviceName = request.Url.AbsoluteUri;
var requestType = MethodConver(request.HttpMethod); var inputParams = ReadStream(await httpRequestMessage.Content.ReadAsStreamAsync());
var outputParams = ReadStream(await httpResponseMessage.Content.ReadAsStreamAsync()); //在这里使用以上变量记录日志操作
} private static int MethodConver(string method)
{
switch (method.ToUpper())
{
case "GET":
return 0;
case "POST":
return 1;
case "PUT":
return 2;
case "DELETE":
return 3;
default:
break;
}
return 0;
} public static string ReadStream(Stream stream)
{
var content = string.Empty;
if (stream != null)
{
stream.Seek(0, SeekOrigin.Begin);
int len = (int)stream.Length;
byte[] inputByts = new byte[len];
stream.Read(inputByts, 0, len);
stream.Position = 0;
content = Encoding.UTF8.GetString(inputByts);
} return content;
}
}

3、WebApiConfig下增加代码  config.Filters.Add(new MonitorApiAttribute());

  public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services // Web API routes
config.MapHttpAttributeRoutes(); config.Filters.Add(new MonitorApiAttribute()); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}

ActionFilterAttribute 全局记录API日志的更多相关文章

  1. 基于AOP和ThreadLocal实现的一个简单Http API日志记录模块

    Log4a 基于AOP和ThreadLocal实现的一个简单Http API日志记录模块 github地址 : https://github.com/EalenXie/log4a 在API每次被请求时 ...

  2. SLF4J - 一个允许你统一日志记录API的抽象层

    一.什么是SLF4J 我们在做Java开发时,如果需要记录日志,有很多日志API可供选择,如: java.util.logging Apache log4j logback SLF4J又是个什么东东呢 ...

  3. asp.net core全局异常过滤并监控系统BUG将异常信息记录到日志

    添加: using Dw.Util.Helper; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collect ...

  4. .NET CORE之API日志收集

    我们在构建WEBAPI项目时,通常需要构建一个全局的记录API 请求和返回 的功能,在WEBAPI框架下 我们通过自定义一个DelegateHandler来实现这个功能, 在.NET CORE框架下已 ...

  5. 基于.NetCore3.1系列 —— 日志记录之日志配置揭秘

    一.前言 在项目的开发维护阶段,有时候我们关注的问题不仅仅在于功能的实现,甚至需要关注系统发布上线后遇到的问题能否及时的查找并解决.所以我们需要有一个好的解决方案来及时的定位错误的根源并做出正确及时的 ...

  6. MVC 记录操作日志与过滤特殊字符

    最近进行的MVC系统需要用到记录操作日志和过滤特殊字符的功能,如果每个action中都调用记录日志的方法就太麻烦了,所以根据需要结合mvc的过滤机制 写了个特殊字符验证与记录操作日志的公用类: pub ...

  7. DB2不记录事务日志

    1. DB2大数据处理不记录事务日志步骤:  建表需要添加属性“NOT LOGGED INITIALLY”  在大批量更改操作的同一个事务开始时执行:“ALTER TABLE tabname ACTI ...

  8. MVC4.0 利用HandleErrorAttribute和log4net实现记录异常日志功能

    1.MVC4.0中HandleErrorAttribte已经帮我们处理了异常问题,当我们新建一个非空的MVC项目时候,在FilterConfig中会发现这样的代码 public class Filte ...

  9. Serilog记录MongoDB日志报错:requires the binary sub type to be UuidLegacy, not UuidStandard

    Serilog Serilog是.NET开源结构化日志类库 开源地址:https://github.com/serilog 官网:https://serilog.net/ Serilog能做什么: 记 ...

随机推荐

  1. svg 动画 透明度 放大缩小 x轴Y轴

    参考链接:https://www.cnblogs.com/Chrimisia/p/6670303.html vue 中封装svg:http://www.cnblogs.com/Jiangchuanwe ...

  2. git教程:撤销修改

    转载:撤销修改 自然,你是不会犯错的.不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行: $ cat readme.txt Git is a distributed ve ...

  3. [LeetCode&Python] Problem 671. Second Minimum Node In a Binary Tree

    Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...

  4. 非root用户安装python3

    1.下载源码 wget -c https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz 解压: tar xzf Python-3.7.1.tgz ...

  5. Springboot 使用过滤器进行加密解密(二)

    之前写过一篇关于过滤器实现加密解密功能的文章,但是在实际开发业务中发现,还是有一些问题的,在此特地说明. 第一:过滤器走两遍的问题: 1.过滤器上,添加了两个注解 第一个:@Compent   将此F ...

  6. FixedUpdate()使用

    当MonoBehaviour启用时,其 FixedUpdate在每一帧被调用. 处理Rigidbody时,需要用FixedUpdate代替Update.例如:给刚体加一个作用力时,你必须应用作用力在F ...

  7. Springboot整合Mybatis-puls

    Spring boot对于我来说是一个刚接触的新东西,学习过程中,发现这东西还是很容易上手的,Spring boot没配置时会默认使用Spring data jpa,这东西可以说一个极简洁的工具,可是 ...

  8. jsp的四个作用域page、request、session、application

    1.简单说 page指当前页面.在一个jsp页面里有效 2.request 指从http请求到服务器处理结束,返回响应的整个过程.在这个过程中使用forward方式跳转多个jsp.在这些页面里你都可以 ...

  9. 【bug记录】OS Lab3 踩坑记

    OS Lab3 踩坑记 Lab3在之前Lab2的基础上,增加了进程建立.调度和中断异常处理.其中测试包括进程建立以及进程调度部分. 由于是第一次做bug记录,而且是调试完bug后再做的记录,所以导致记 ...

  10. django 分页组件

      一.仿django分页功能自己实现 urls.py 1 2 3 4 5 6 7 8 9 from django.conf.urls import url from django.contrib i ...