ActionFilterAttribute 全局记录API日志
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日志的更多相关文章
- 基于AOP和ThreadLocal实现的一个简单Http API日志记录模块
Log4a 基于AOP和ThreadLocal实现的一个简单Http API日志记录模块 github地址 : https://github.com/EalenXie/log4a 在API每次被请求时 ...
- SLF4J - 一个允许你统一日志记录API的抽象层
一.什么是SLF4J 我们在做Java开发时,如果需要记录日志,有很多日志API可供选择,如: java.util.logging Apache log4j logback SLF4J又是个什么东东呢 ...
- asp.net core全局异常过滤并监控系统BUG将异常信息记录到日志
添加: using Dw.Util.Helper; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collect ...
- .NET CORE之API日志收集
我们在构建WEBAPI项目时,通常需要构建一个全局的记录API 请求和返回 的功能,在WEBAPI框架下 我们通过自定义一个DelegateHandler来实现这个功能, 在.NET CORE框架下已 ...
- 基于.NetCore3.1系列 —— 日志记录之日志配置揭秘
一.前言 在项目的开发维护阶段,有时候我们关注的问题不仅仅在于功能的实现,甚至需要关注系统发布上线后遇到的问题能否及时的查找并解决.所以我们需要有一个好的解决方案来及时的定位错误的根源并做出正确及时的 ...
- MVC 记录操作日志与过滤特殊字符
最近进行的MVC系统需要用到记录操作日志和过滤特殊字符的功能,如果每个action中都调用记录日志的方法就太麻烦了,所以根据需要结合mvc的过滤机制 写了个特殊字符验证与记录操作日志的公用类: pub ...
- DB2不记录事务日志
1. DB2大数据处理不记录事务日志步骤: 建表需要添加属性“NOT LOGGED INITIALLY” 在大批量更改操作的同一个事务开始时执行:“ALTER TABLE tabname ACTI ...
- MVC4.0 利用HandleErrorAttribute和log4net实现记录异常日志功能
1.MVC4.0中HandleErrorAttribte已经帮我们处理了异常问题,当我们新建一个非空的MVC项目时候,在FilterConfig中会发现这样的代码 public class Filte ...
- Serilog记录MongoDB日志报错:requires the binary sub type to be UuidLegacy, not UuidStandard
Serilog Serilog是.NET开源结构化日志类库 开源地址:https://github.com/serilog 官网:https://serilog.net/ Serilog能做什么: 记 ...
随机推荐
- 修改XAMPP的默认根目录
XAMPP安装完成后,默认根目录路径是C:\xampp\htdocs,如果想要在服务器下运行文件就必须把该文件copy到C:\xampp\htdocs下.超麻烦不说,公司代码总不能放进去运行吧...所 ...
- WEB学习笔记11-高可读性的HTML之如何设置网页标题层级
标题标签指的是<h1>~<h6>这6个标签,统称为<hx>标签. (1)在页面内容的标题部分使用<hx>标签 <h1 class="re ...
- 小伙 zwfw-new.hunan.gov.cn.iname.damddos.com [222.240.80.52]
由于这个应用出问题非常影响用户体验:于是立马让运维保留现场 dump 线程和内存同时重启应用,还好重启之后恢复正常.于是开始着手排查问题.
- JavaSE-2018.12.20更新
JDK:Java Development Kit(java开发工具包,是提供给java开发人员使用的,包含了开发工具(编译工具javac.exe和打包工具jar.exe等)+JRE) JRE:Java ...
- ArcMap图层属性表中添加图片
一看标题是不是有点懵?懵就对了!刚接触到的时候我也有点懵,属性表不是都是文本啊数字啊之类的格式,怎么还可以存图片,下面就带大家来看看吧! 一.关于图层入库问题 图层进入数据库和图层以shp格式存储时, ...
- XAF创建一个DashBoard
1.首先启动windows程序之后点击DashBoard导航栏 2.接着点击新建按钮,开始创建一个DashBoard 3.接着根据你的数据来源选择数据源,这里我选择了数据库 4.接着填好你的服务器和数 ...
- java的AES对称加密和解密,有偏移量
import java.math.BigDecimal; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; i ...
- nexus 随笔
离线更新中央仓库索引的方式,速度快并且可靠. 访问http://repo.maven.apache.org/maven2/.index/下载中心仓库最新版本的索引文件,我们需要下载如下两个文件nexu ...
- vue day5 分页控件
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- hbase整合
hbase與hive整合 1. hive中有數據 --> 創建hive管理表映射hbase 例如: 1)hive創建內部表 create tabl ...