- 定义一个TraceFilter类
- 具体的Controller Action引用这个类就行了[TraceFilter]。
比较简单,直接上代码:
public class TraceFilter:ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext context) { context.HttpContext.Items["start"] = DateTime.Now; } public override void OnResultExecuted(ResultExecutedContext context) { DateTime start = (DateTime)context.HttpContext.Items["start"]; TimeSpan diff = DateTime.Now - start; File.AppendAllText(@"c:log.txt", diff.ToString()+" "); } }