public class OperateLogFilter : ActionFilterAttribute { private Stopwatch Stopwatch { get; set; } public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); Stopwatch = new Stopwatch(); Stopwatch.Start(); } public override void OnActionExecuted(ActionExecutedContext context) { base.OnActionExecuted(context); Stopwatch.Stop(); string apiName = context.ActionDescriptor.DisplayName; double elapsed = Stopwatch.Elapsed.TotalMilliseconds; // 获取请求参数 HttpRequest request = context.HttpContext.Request; //-- 由于mvc里已经读过request.Body,现在它的position在末尾 // 允许重新读body request.EnableRewind(); // 将position置到开始位置 request.Body.Position = 0; // 读取body的数据流,并转为string var reader = new StreamReader(request.Body); var contentFromBody = reader.ReadToEnd(); } }